Verified Commit 8ae2882d authored by Andrej Shadura's avatar Andrej Shadura
Browse files

Refactor: less CAPS, more pathlib and TemporaryDirectory



Signed-off-by: Andrej Shadura's avatarAndrej Shadura <andrew.shadura@collabora.co.uk>
parent 4aae0f58
......@@ -7,13 +7,9 @@ import osc.cmdln as cmdln
import osc.conf as conf
import osc.core
try:
from debian.deb822 import Changes, Dsc
except ImportError:
from deb822 import Changes, Dsc
import shutil
import atexit
import tempfile
from debian.deb822 import Changes, Dsc
from tempfile import TemporaryDirectory
from pathlib import Path
def _get_objects_from_file(filename):
"""
......@@ -56,21 +52,10 @@ def _get_objects_from_file(filename):
return changes, dsc, dscfile
class DPut(object):
WORKING_DIR = None
PACKAGE_NAME = None
PROJECT_NAME = None
def _create_and_move_to_working_dir(self):
"""
Create and move the current_path (CWD) to the working_dir
"""
working_path = self.WORKING_DIR
# TODO: Should we check for IO exceptions here?
if not os.path.exists(working_path):
os.mkdir(working_path)
os.chdir(working_path)
def __init__(self, project_name=None, package_name=None, working_dir=None):
self.project_name = project_name
self.package_name = package_name
self.working_dir = Path(working_dir)
def _create_package(self):
"""
......@@ -78,10 +63,10 @@ class DPut(object):
"""
current_path = os.getcwd()
project_path = os.path.join(self.WORKING_DIR, self.PROJECT_NAME)
project_path = os.path.join(self.working_dir, self.project_name)
os.chdir(project_path)
osc.core.createPackageDir(self.PACKAGE_NAME)
osc.core.createPackageDir(self.package_name)
project = osc.core.Project(".", getPackageList=False)
project.update()
......@@ -96,7 +81,7 @@ class DPut(object):
# this one is needed because there's two ways of getting the package object
# first one fails if the folder already exists, the second one fail if it doesn't
path = os.path.join(self.WORKING_DIR, self.PROJECT_NAME, self.PACKAGE_NAME)
path = os.path.join(self.working_dir, self.project_name, self.package_name)
if os.path.exists(path) and os.path.isdir(path):
os.chdir(path)
......@@ -105,8 +90,8 @@ class DPut(object):
elif not os.path.exists(path):
package = osc.core.Package.init_package(conf.config['apiurl'],
project=self.PROJECT_NAME,
package=self.PACKAGE_NAME,
project=self.project_name,
package=self.package_name,
dir=path,
meta=True)
package.update()
......@@ -122,30 +107,20 @@ class DPut(object):
"""
Returns a list of files inside an specific package
"""
fileList = osc.core.meta_get_filelist(conf.config['apiurl'], self.PROJECT_NAME, self.PACKAGE_NAME)
fileList = osc.core.meta_get_filelist(conf.config['apiurl'], self.project_name, self.package_name)
return fileList
@cmdln.option('--maintained-in-git', action='store_true',
help='add MAINTAINED_IN_GIT.txt')
def do_dput(self, subcmd, opts, proj_name, dsc_or_changes_file):
dput = DPut()
dput.PROJECT_NAME = proj_name.encode("utf8")
dput.WORKING_DIR = tempfile.mkdtemp('_oscdput')
def cleanup(dirname, initdir):
os.chdir(initdir)
if os.path.exists(dirname):
shutil.rmtree(dirname)
atexit.register(cleanup, dirname=dput.WORKING_DIR, initdir=os.getcwd())
with TemporaryDirectory('_oscdput') as working_dir:
# get debian .change object before moving current path to the
# working_dir
changes, dsc, dsc_file = _get_objects_from_file(dsc_or_changes_file)
dput.PACKAGE_NAME = dsc.get("Source").encode('utf8') # XXX: needs to be utf8!!!
dput.PACKAGE_VERSION = dsc.get("Version")
package_name = dsc.get("Source")
dput = DPut(proj_name, package_name, working_dir)
# Filenames in the .dsc are relative to the directory where it appears.
# We need to make it absolute before we chdir elsewhere.
......@@ -153,20 +128,18 @@ def do_dput(self, subcmd, opts, proj_name, dsc_or_changes_file):
dsc_file = os.path.abspath(dsc_file)
dscdir = os.path.dirname(dsc_file)
dput._create_and_move_to_working_dir()
# Get the list of packages
# TODO: Probably it can be done after checking out the project
# So we can save one http request
package_list = osc.core.meta_get_packagelist(conf.config['apiurl'], dput.PROJECT_NAME)
package_list = osc.core.meta_get_packagelist(conf.config['apiurl'], proj_name)
# Starting the project
project = osc.core.Project.init_project(conf.config['apiurl'],
dir=dput.PROJECT_NAME,
project=dput.PROJECT_NAME)
dir=dput.working_dir / proj_name,
project=proj_name)
# check if the package exists on server, otherwise create one
if dput.PACKAGE_NAME not in package_list:
if package_name not in package_list:
dput._create_package()
# it also changes the current_dir to the package dir
......@@ -184,7 +157,7 @@ def do_dput(self, subcmd, opts, proj_name, dsc_or_changes_file):
for f in remote_file_list:
if f.endswith('.dsc'):
u = osc.core.makeurl(conf.config['apiurl'],
['source', dput.PROJECT_NAME, dput.PACKAGE_NAME, f],
['source', proj_name, package_name, f],
query={})
lines = []
......@@ -205,7 +178,7 @@ def do_dput(self, subcmd, opts, proj_name, dsc_or_changes_file):
# The temporary checkout we're using doesn't seem to have the
# expected result for latest_rev()?
source_rev = osc.core.get_source_rev(conf.config['apiurl'],
dput.PROJECT_NAME, dput.PACKAGE_NAME)
proj_name, package_name)
if 'rev' in source_rev:
latest_revision = source_rev['rev']
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment