Merge pull request #4722 from Gallaecio/umask

Do not let umask affect the permissions of startproject-generated files
This commit is contained in:
Andrey Rahmatullin 2020-08-28 18:41:02 +05:00 committed by GitHub
commit 59a0157ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 5 deletions

View File

@ -12,10 +12,12 @@ def render_templatefile(path, **kwargs):
content = string.Template(raw).substitute(**kwargs)
render_path = path[:-len('.tmpl')] if path.endswith('.tmpl') else path
if path.endswith('.tmpl'):
os.rename(path, render_path)
with open(render_path, 'wb') as fp:
fp.write(content.encode('utf8'))
if path.endswith('.tmpl'):
os.remove(path)
CAMELCASE_INVALID_CHARS = re.compile(r'[^a-zA-Z\d]')

View File

@ -128,9 +128,13 @@ class StartprojectTest(ProjectTest):
def get_permissions_dict(path, renamings=None, ignore=None):
def get_permissions(path):
return oct(os.stat(path).st_mode)
renamings = renamings or tuple()
permissions_dict = {
'.': os.stat(path).st_mode,
'.': get_permissions(path),
}
for root, dirs, files in os.walk(path):
nodes = list(chain(dirs, files))
@ -145,13 +149,15 @@ def get_permissions_dict(path, renamings=None, ignore=None):
search_string,
replacement
)
permissions = os.stat(absolute_path).st_mode
permissions = get_permissions(absolute_path)
permissions_dict[relative_path] = permissions
return permissions_dict
class StartprojectTemplatesTest(ProjectTest):
maxDiff = None
def setUp(self):
super().setUp()
self.tmpl = join(self.temp_path, 'templates')
@ -293,7 +299,7 @@ class StartprojectTemplatesTest(ProjectTest):
path.mkdir(mode=permissions)
else:
path.touch(mode=permissions)
expected_permissions[node] = path.stat().st_mode
expected_permissions[node] = oct(path.stat().st_mode)
process = subprocess.Popen(
(
@ -313,6 +319,53 @@ class StartprojectTemplatesTest(ProjectTest):
self.assertEqual(actual_permissions, expected_permissions)
def test_startproject_permissions_umask_022(self):
"""Check that generated files have the right permissions when the
system uses a umask value that causes new files to have different
permissions than those from the template folder."""
@contextmanager
def umask(new_mask):
cur_mask = os.umask(new_mask)
yield
os.umask(cur_mask)
scrapy_path = scrapy.__path__[0]
project_template = os.path.join(
scrapy_path,
'templates',
'project'
)
project_name = 'umaskproject'
renamings = (
('module', project_name),
('.tmpl', ''),
)
expected_permissions = get_permissions_dict(
project_template,
renamings,
IGNORE,
)
with umask(0o002):
destination = mkdtemp()
process = subprocess.Popen(
(
sys.executable,
'-m',
'scrapy.cmdline',
'startproject',
project_name,
),
cwd=destination,
env=self.env,
)
process.wait()
project_dir = os.path.join(destination, project_name)
actual_permissions = get_permissions_dict(project_dir)
self.assertEqual(actual_permissions, expected_permissions)
class CommandTest(ProjectTest):