_TmpDir   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __exit__() 0 11 3
A __init__() 0 2 1
A __enter__() 0 2 1
1
from __future__ import print_function
2
3
import logging
4
import os
5
import shutil
6
import sys
7
import tempfile
8
9
from binstar_client import errors
10
11
logger = logging.getLogger('binstar.projects.upload')
12
13
14
class _TmpDir(object):
15
    def __init__(self, prefix):
16
        self._dir = tempfile.mkdtemp(prefix=prefix)
17
18
    def __exit__(self, type, value, traceback):
19
        try:
20
            shutil.rmtree(path=self._dir)
21
        except Exception as e:
22
            # prefer original exception to rmtree exception
23
            if value is None:
24
                print("Exception cleaning up TmpDir %s: %s" % (self._dir, str(e)), file=sys.stderr)
25
                raise e
26
            else:
27
                print("Failed to clean up TmpDir %s: %s" % (self._dir, str(e)), file=sys.stderr)
28
                raise value
29
30
    def __enter__(self):
31
        return self._dir
32
33
34
def _real_upload_project(project, args, username):
35
    from anaconda_project import project_ops
36
37
    print("Uploading project: {}".format(project.name))
38
39
    status = project_ops.upload(project, site=args.site, username=username,
40
                                token=args.token, log_level=args.log_level)
41
42
    for log in status.logs:
43
        print(log)
44
    if not status:
45
        for error in status.errors:
46
            print(error, file=sys.stderr)
47
        print(status.status_description, file=sys.stderr)
48
        raise errors.BinstarError(status.status_description)
49
    else:
50
        print(status.status_description)
51
        return [project.name, status.url]
52
53
54
def upload_project(project_path, args, username):
55
    try:
56
        from anaconda_project import project_ops
57
    except ImportError:
58
        raise errors.BinstarError("To upload projects such as {}, install the anaconda-project package.".format(project_path))
59
60
    from anaconda_project import project
61
62
    if os.path.exists(project_path) and not os.path.isdir(project_path):
63
        # make the single file into a temporary project directory
64
        with (_TmpDir(prefix="anaconda_upload_")) as dirname:
65
            shutil.copy(project_path, dirname)
66
            basename_no_extension = os.path.splitext(os.path.basename(project_path))[0]
67
            project = project_ops.create(dirname, name=basename_no_extension)
68
            return _real_upload_project(project, args, username)
69
    else:
70
        project = project.Project(directory_path=project_path)
71
        return _real_upload_project(project, args, username)
72