Skip to content

Commit

Permalink
pack: added .packignore
Browse files Browse the repository at this point in the history
In some cases, there are files that may be useful, but should not
be part of artifact. The ability to add files and directories to
the .packignore file has been added, which allows you to ignore
these files and directories when packing.

Closes: [812](#812)
  • Loading branch information
Japsty committed Oct 28, 2024
1 parent 1cb65d9 commit b93703e
Show file tree
Hide file tree
Showing 42 changed files with 250 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cli/pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
}
}

projectPath := filepath.Dir(packCtx.configFilePath)
ignorePatterns, err := readPackIgnore(projectPath)
if err != nil {
return "", fmt.Errorf("failed to read .packignore: %v", err)
}

if err = removeIgnoredFiles(bundleEnvPath, ignorePatterns); err != nil {
return "", fmt.Errorf("failed to remove ignored files: %v", err)
}

if buildRocks {
err = buildAppRocks(cmdCtx, packCtx, cliOpts, bundleEnvPath)
if err != nil && !os.IsNotExist(err) {
Expand Down
20 changes: 20 additions & 0 deletions cli/pack/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,26 @@ func Test_prepareBundle(t *testing.T) {
{assert.FileExists, "app/tt.yaml"},
},
},
{
name: "Packing env with packignore",
params: params{
configPath: "testdata/bundle_with_packignore/tt.yaml",
tntExecutable: tntExecutable,
packCtx: PackCtx{Name: "app"},
},
wantErr: false,
checks: []check{
{assert.DirExists, "instances.enabled"},
{assert.NoFileExists, "instances.enabled/app"},

{assert.NoDirExists, "modules"},

{assert.DirExists, "app2"},
{assert.NoDirExists, "app2/var"},

{assert.NoFileExists, "app.lua"},
},
},
}

for _, tt := range tests {
Expand Down
100 changes: 100 additions & 0 deletions cli/pack/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package pack

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)

// readPackIgnore reads the .packignore file and returns a slice of ignore patterns.
func readPackIgnore(projectPath string) (map[string]struct{}, error) {
ignoreFilePath := filepath.Join(projectPath, ".packignore")
file, err := os.Open(ignoreFilePath)
if err != nil {
if os.IsNotExist(err) {
return map[string]struct{}{}, nil
}
return nil, err
}
defer file.Close()

patterns := make(map[string]struct{})
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
patterns[line] = struct{}{}
}

if err := scanner.Err(); err != nil {
return nil, err
}
return patterns, nil
}

// shouldIgnore checks if the given file path matches any of the ignore patterns.
func shouldIgnore(path string, patterns map[string]struct{}) (bool, error) {
for pattern := range patterns {
pattern = filepath.ToSlash(pattern)
filePath := filepath.ToSlash(path)

if strings.HasSuffix(pattern, "/") {
if strings.HasPrefix(filePath, pattern) {
return true, nil
}
continue
}

match, err := filepath.Match(pattern, filePath)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}

// removeIgnoredFiles walks through the directory and removes files or directories
// that match the ignore patterns.
func removeIgnoredFiles(bundleEnvPath string, patterns map[string]struct{}) error {
return filepath.Walk(bundleEnvPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

relPath, err := filepath.Rel(bundleEnvPath, path)
if err != nil {
return err
}

relPathUnix := filepath.ToSlash(relPath)

ignore, err := shouldIgnore(relPathUnix, patterns)
if err != nil {
return err
}

if ignore {
if info.IsDir() {
err = os.RemoveAll(path)
if err != nil {
return fmt.Errorf("failed to remove directory %q: %v", path, err)
}
return filepath.SkipDir
} else {
err = os.Remove(path)
if err != nil {
return fmt.Errorf("failed to remove file %q: %v", path, err)
}
}
}
return nil
})
}
4 changes: 4 additions & 0 deletions cli/pack/testdata/bundle_with_packignore/.packignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instances.enabled/app
modules
app2/var
app.lua
Empty file.
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/app2
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/instances_enabled
1 change: 1 addition & 0 deletions cli/pack/testdata/bundle_with_packignore/modules
11 changes: 11 additions & 0 deletions cli/pack/testdata/bundle_with_packignore/tt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
bin_dir: bin
inc_dir: include
instances_enabled: .
tarantoolctl_layout: false
app:
run_dir: var/run
log_dir: var/log
wal_dir: var/lib
memtx_dir: var/lib
vinyl_dir: var/lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instances.enabled/app1
modules
app2/var
app.lua
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = 'app2'
version = 'scm-1'
source = {
url = '/dev/null',
}
-- Put any modules your app depends on here
dependencies = {
'tarantool',
'lua >= 5.1',
}
build = {
type = 'none';
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inst1:

inst2:
Empty file.
11 changes: 11 additions & 0 deletions test/integration/pack/test_bundles/bundle_with_packignore/tt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
env:
instances_enabled: instances_enabled
restart_on_failure: true
modules:
directory: modules
app:
run_dir: var/run
log_dir: var/log
wal_dir: var/lib
memtx_dir: var/lib
vinyl_dir: var/lib
52 changes: 52 additions & 0 deletions test/integration/pack/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,3 +1836,55 @@ def test_pack_app_local_tarantool(tt_cmd, tmpdir_with_tarantool, tmp_path):

build_output = tt_process.stdout.read()
assert "Bundle is packed successfully" in build_output


@pytest.mark.docker
def test_pack_deb_packignore(tt_cmd, tmp_path):
if shutil.which('docker') is None:
pytest.skip("docker is not installed in this system")

# check if docker daemon is up
rc, _ = run_command_and_get_output(['docker', 'ps'])
assert rc == 0

tmp_path = os.path.join(tmp_path, "bundle_with_packignore")
shutil.copytree(os.path.join(os.path.dirname(__file__), "test_bundles",
"bundle_with_packignore"),
tmp_path, symlinks=True, ignore=None,
copy_function=shutil.copy2, ignore_dangling_symlinks=True,
dirs_exist_ok=True)

base_dir = tmp_path

rc, output = run_command_and_get_output(
[tt_cmd, "pack", "deb"],
cwd=base_dir, env=dict(os.environ, PWD=tmp_path))
assert rc == 0

package_file_name = "bundle_with_packignore_0.1.0.0-1_" + get_arch() + ".deb"
package_file = os.path.join(base_dir, package_file_name)
assert os.path.isfile(package_file)

unpacked_pkg_dir = os.path.join(tmp_path, 'unpacked')
os.mkdir(unpacked_pkg_dir)

rc, output = run_command_and_get_output(['docker', 'run', '--rm', '-v',
'{0}:/usr/src/'.format(base_dir),
'-v', '{0}:/tmp/unpack'.format(unpacked_pkg_dir),
'-w', '/usr/src',
'jrei/systemd-ubuntu',
'/bin/bash', '-c',
'/bin/dpkg -i {0}'
'&& id tarantool '
' && dpkg -x {0} /tmp/unpack '
' && chown {1}:{2} /tmp/unpack -R'.
format(package_file_name, os.getuid(), os.getgid())
])
assert rc == 0

env_path = os.path.join(unpacked_pkg_dir, 'usr', 'share', 'tarantool', 'bundle_with_packignore')
for path in ['tt.yaml', 'instances.enabled', 'app2', 'bin/tt', 'bin/tarantool']:
assert os.path.exists(os.path.join(env_path, path))

for path in ['modules', 'app.lua', 'app2/var', 'instances.enabled/app1']:
assert not os.path.exists(os.path.join(env_path, path))

0 comments on commit b93703e

Please sign in to comment.