Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigate Name clash on extract / fix cache handling #327

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TarSCM/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ def create_archive(self, scm_object, **kwargs):
# write meta data
metafile = open(os.path.join(args.outdir, basename + '.obsinfo'), "w")
metafile.write("name: " + basename + "\n")
metafile.write(
"scmdir: " + os.path.basename(scm_object.clone_dir) + "\n")
metafile.write("version: " + version + "\n")
metafile.write("mtime: " + str(tstamp) + "\n")

Expand Down
13 changes: 9 additions & 4 deletions TarSCM/scm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ def detect_changes_scm(self, subdir, chgs):

def get_repocache_hash(self, subdir):
"""Calculate hash fingerprint for repository cache."""
u_url = self.url.encode()
return hashlib.sha256(u_url).hexdigest()
# tar has no u_url
if self.url:
u_url = self.url.encode()
return hashlib.sha256(u_url).hexdigest()
else:
return None

def get_current_commit(self):
return None
Expand All @@ -162,7 +166,8 @@ def _calc_repocachedir(self):
if repocachedir:
logging.debug("REPOCACHE: %s", repocachedir)
self.repohash = self.get_repocache_hash(self.args.subdir)
self.repocachedir = os.path.join(repocachedir, self.repohash)
if self.repohash:
self.repocachedir = os.path.join(repocachedir, self.repohash)

def _calc_proxies(self):
# check for standard http/https proxy variables
Expand Down Expand Up @@ -230,7 +235,7 @@ def _calc_dir_to_clone_to(self, prefix):
else:
tempdir = os.getcwd()

self.repodir = os.path.join(tempdir, self.basename)
self.repodir = os.path.join(tempdir, self.basename + '_service')

if self.repocachedir:
# Update atime and mtime of repocachedir to make it easier
Expand Down
15 changes: 8 additions & 7 deletions TarSCM/scm/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def fetch_upstream(self):
if self.args.obsinfo is None:
raise SystemExit("ERROR: no .obsinfo file found in directory: "
"'%s'" % os.getcwd())
self.basename = self.clone_dir = self.read_from_obsinfo(
self.args.obsinfo,
"name"
)
self.basename = self.read_from_obsinfo(self.args.obsinfo, "name")
self.clone_dir = self.read_from_obsinfo(self.args.obsinfo, "scmdir")
# for backwards compatibility - where scmdir is not set
if not self.clone_dir:
self.clone_dir = self.basename
if "/" in self.clone_dir:
sys.exit("name in obsinfo contains '/'.")

Expand All @@ -30,18 +31,18 @@ def fetch_upstream(self):
if "/" in version or '..' in version:
sys.exit("verion in obsinfo contains '/' or '..'.")

self.clone_dir += "-" + version
self.basename += "-" + version

if not os.path.exists(self.clone_dir):
self._final_rename_needed = True
# not need in case of local osc build
try:
os.rename(self.basename, self.clone_dir)
os.rename(self.clone_dir, self.basename)
except OSError:
raise SystemExit(
"Error while moving from '%s' to '%s')\n"
"Current working directory: '%s'" %
(self.basename, self.clone_dir, os.getcwd())
(self.clone_dir, self.basename, os.getcwd())
)

def update_cache(self):
Expand Down
2 changes: 1 addition & 1 deletion TarSCM/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def process_single_task(self, args):
if args.filename:
dstname = basename = args.filename
else:
dstname = basename = os.path.basename(scm_object.clone_dir)
dstname = basename = os.path.basename(scm_object.basename)

version = self.get_version()
changesversion = version
Expand Down
3 changes: 2 additions & 1 deletion tests/tartests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def test_tar_scm_finalize(self):
obsinfo = open(info, 'w')
obsinfo.write(
"name: pkgname\n" +
"scmdir: pkgname_service\n" +
"version: 0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
)
obsinfo.close()
src_dir = os.path.join(wdir, "pkgname")
src_dir = os.path.join(wdir, "pkgname_service")
os.mkdir(src_dir)
self.tar_scm_std()
self.assertTrue(os.path.isdir(src_dir))
Expand Down
7 changes: 6 additions & 1 deletion tests/unittestcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_calc_dir_to_clone_to(self):
for cdir in clone_dirs:
scm.url = cdir
scm._calc_dir_to_clone_to("") # pylint: disable=protected-access
self.assertTrue(scm.clone_dir.endswith('/repo'))
self.assertTrue(scm.clone_dir.endswith('/repo_service'))
self.tasks.cleanup()

@patch('TarSCM.Helpers.safe_run')
Expand Down Expand Up @@ -205,6 +205,7 @@ def test_scm_tar(self):
f_h = open(info, 'w')
f_h.write(
"name: test\n" +
"scmdir: test_service\n" +
"version: 0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
Expand All @@ -227,6 +228,7 @@ def test_scm_tar(self):
f_h = open(info, 'w')
f_h.write(
"name: nonexistantbase\n" +
"scmdir: nonexistantbase_service\n" +
"version: 0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
Expand Down Expand Up @@ -344,6 +346,7 @@ def test_scm_tar_invalid_params(self):
f_h = open(info, 'w')
f_h.write(
"name: test/test\n" +
"scmdir: test/test_service\n" +
"version: 0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
Expand All @@ -361,6 +364,7 @@ def test_scm_tar_invalid_params(self):
f_h = open(info, 'w')
f_h.write(
"name: test\n" +
"scmdir: test_service\n" +
"version: a/0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
Expand All @@ -378,6 +382,7 @@ def test_scm_tar_invalid_params(self):
f_h = open(info, 'w')
f_h.write(
"name: test\n" +
"scmdir: test_service\n" +
"version: ..0.1.1\n" +
"mtime: 1476683264\n" +
"commit: fea6eb5f43841d57424843c591b6c8791367a9e5\n"
Expand Down