Merge lp:~sergiusens/snapcraft/split-up-collect into lp:~snappy-dev/snapcraft/core

Proposed by Sergio Schvezov
Status: Merged
Approved by: Leo Arias
Approved revision: 144
Merged at revision: 144
Proposed branch: lp:~sergiusens/snapcraft/split-up-collect
Merge into: lp:~snappy-dev/snapcraft/core
Diff against target: 106 lines (+56/-34)
1 file modified
snapcraft/plugin.py (+56/-34)
To merge this branch: bzr merge lp:~sergiusens/snapcraft/split-up-collect
Reviewer Review Type Date Requested Status
Leo Arias (community) Approve
Review via email: mp+269519@code.launchpad.net

Commit message

Reducing mccabe complexity from 10 to 3 for PluginHandler.collect_snap_files

Description of the change

From
python3 -m mccabe --min 10 snapcraft/plugin.py
215:1: 'PluginHandler.collect_snap_files' 10

To
python3 -m mccabe --min 3 snapcraft/plugin.py
215:1: 'PluginHandler.collect_snap_files' 3

The split out functions are at 5 for the highest.

This is all tested by collect_snap_files' tests

To post a comment you must log in.
Revision history for this message
Leo Arias (elopio) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snapcraft/plugin.py'
2--- snapcraft/plugin.py 2015-08-26 08:52:09 +0000
3+++ snapcraft/plugin.py 2015-08-28 15:41:33 +0000
4@@ -214,42 +214,16 @@
5
6 def collect_snap_files(self, includes, excludes):
7 # validate
8- for d in includes + excludes:
9- if os.path.isabs(d):
10- raise PluginError("path '{}' must be relative".format(d))
11-
12- sourceFiles = set()
13- for root, dirs, files in os.walk(self.installdir):
14- sourceFiles |= set([os.path.join(root, d) for d in dirs])
15- sourceFiles |= set([os.path.join(root, f) for f in files])
16- sourceFiles = set([os.path.relpath(x, self.installdir) for x in sourceFiles])
17-
18- includeFiles = set()
19- for include in includes:
20- matches = glob.glob(os.path.join(self.stagedir, include))
21- includeFiles |= set(matches)
22- includeDirs = [x for x in includeFiles if os.path.isdir(x)]
23- includeFiles = set([os.path.relpath(x, self.stagedir) for x in includeFiles])
24-
25- # Expand includeFiles, so that an exclude like '*/*.so' will still match
26- # files from an include like 'lib'
27- for includeDir in includeDirs:
28- for root, dirs, files in os.walk(includeDir):
29- includeFiles |= set([os.path.relpath(os.path.join(root, d), self.stagedir) for d in dirs])
30- includeFiles |= set([os.path.relpath(os.path.join(root, f), self.stagedir) for f in files])
31-
32- # Grab exclude list
33- excludeFiles = set()
34- for exclude in excludes:
35- matches = glob.glob(os.path.join(self.stagedir, exclude))
36- excludeFiles |= set(matches)
37- excludeDirs = [os.path.relpath(x, self.stagedir) for x in excludeFiles if os.path.isdir(x)]
38- excludeFiles = set([os.path.relpath(x, self.stagedir) for x in excludeFiles])
39+ _validate_relative_paths(includes + excludes)
40+
41+ source_files = _generate_source_set(self.installdir)
42+ include_files = _generate_include_set(self.stagedir, includes)
43+ exclude_files, exclude_dirs = _generate_exclude_set(self.stagedir, excludes)
44
45 # And chop files, including whole trees if any dirs are mentioned
46- snap_files = (includeFiles & sourceFiles) - excludeFiles
47- for excludeDir in excludeDirs:
48- snap_files = set([x for x in snap_files if not x.startswith(excludeDir + '/')])
49+ snap_files = (include_files & source_files) - exclude_files
50+ for exclude_dir in exclude_dirs:
51+ snap_files = set([x for x in snap_files if not x.startswith(exclude_dir + '/')])
52
53 # Separate dirs from files
54 snap_dirs = set([x for x in snap_files if os.path.isdir(os.path.join(self.stagedir, x)) and not os.path.islink(os.path.join(self.stagedir, x))])
55@@ -269,3 +243,51 @@
56 logger.error('Could not load part %s', plugin_name)
57 sys.exit(1)
58 return part
59+
60+
61+def _generate_source_set(installdir):
62+ source_files = set()
63+ for root, dirs, files in os.walk(installdir):
64+ source_files |= set([os.path.join(root, d) for d in dirs])
65+ source_files |= set([os.path.join(root, f) for f in files])
66+ source_files = set([os.path.relpath(x, installdir) for x in source_files])
67+
68+ return source_files
69+
70+
71+def _generate_include_set(stagedir, includes):
72+ include_files = set()
73+ for include in includes:
74+ matches = glob.glob(os.path.join(stagedir, include))
75+ include_files |= set(matches)
76+
77+ include_dirs = [x for x in include_files if os.path.isdir(x)]
78+ include_files = set([os.path.relpath(x, stagedir) for x in include_files])
79+
80+ # Expand includeFiles, so that an exclude like '*/*.so' will still match
81+ # files from an include like 'lib'
82+ for include_dir in include_dirs:
83+ for root, dirs, files in os.walk(include_dir):
84+ include_files |= set([os.path.relpath(os.path.join(root, d), stagedir) for d in dirs])
85+ include_files |= set([os.path.relpath(os.path.join(root, f), stagedir) for f in files])
86+
87+ return include_files
88+
89+
90+def _generate_exclude_set(stagedir, excludes):
91+ exclude_files = set()
92+
93+ for exclude in excludes:
94+ matches = glob.glob(os.path.join(stagedir, exclude))
95+ exclude_files |= set(matches)
96+
97+ exclude_dirs = [os.path.relpath(x, stagedir) for x in exclude_files if os.path.isdir(x)]
98+ exclude_files = set([os.path.relpath(x, stagedir) for x in exclude_files])
99+
100+ return exclude_files, exclude_dirs
101+
102+
103+def _validate_relative_paths(files):
104+ for d in files:
105+ if os.path.isabs(d):
106+ raise PluginError("path '{}' must be relative".format(d))

Subscribers

People subscribed via source and target branches