Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _parse_relative_bundle_spec(bundle_spec):
"""
Parse bundle spec "BUNDLESPEC^I" into ("BUNDLE_SPEC", I).
:param bundle_spec: string bundle spec
:return: (base bundle spec, reverse history index)
"""
# run: bundle whose name starts with foo
m = spec_util.NAME_PATTERN_REGEX.match(bundle_spec)
if m:
bundle_spec = m.group(1)
reverse_index = 1
return (bundle_spec, reverse_index)
# foo^3: 3rd to last bundle whose name starts with foo
m = spec_util.NAME_PATTERN_HISTORY_REGEX.match(bundle_spec)
if m:
bundle_spec = m.group(1)
reverse_index = int(m.group(2)) if m.group(2) != '' else 1
return (bundle_spec, reverse_index)
# ^3: 3rd to last bundle
m = spec_util.HISTORY_REGEX.match(bundle_spec)
if m:
bundle_spec = None
if (worksheet_spec == '' or worksheet_spec == HOME_WORKSHEET) and user:
worksheet_spec = spec_util.home_worksheet(user.user_name)
if not worksheet_spec:
raise UsageError('Tried to expand empty worksheet_spec!')
if spec_util.UUID_REGEX.match(worksheet_spec):
return worksheet_spec
if spec_util.UUID_PREFIX_REGEX.match(worksheet_spec):
worksheets = model.batch_get_worksheets(
fetch_items=False,
uuid=LikeQuery(worksheet_spec + '%'),
base_worksheet_uuid=base_worksheet_uuid,
)
message = "uuid starting with '%s'" % (worksheet_spec,)
else:
spec_util.check_name(worksheet_spec)
worksheets = model.batch_get_worksheets(
fetch_items=False, name=worksheet_spec, base_worksheet_uuid=base_worksheet_uuid
)
message = "name '%s'" % (worksheet_spec,)
if not worksheets:
raise NotFoundError('No worksheet found with %s' % (message,))
if len(worksheets) > 1:
raise UsageError(
'Found multiple worksheets with %s:%s'
% (message, ''.join('\n %s' % (worksheet,) for worksheet in worksheets))
)
return worksheets[0].uuid
"""
if not request.user.is_authenticated:
raise PermissionError("You must be logged in to create a worksheet.")
ensure_unused_worksheet_name(name)
# Don't need any permissions to do this.
worksheet = Worksheet(
{'name': name, 'title': None, 'frozen': None, 'items': [], 'owner_id': request.user.user_id}
)
local.model.new_worksheet(worksheet)
# Make worksheet publicly readable by default
set_worksheet_permission(worksheet, local.model.public_group_uuid, GROUP_OBJECT_PERMISSION_READ)
if spec_util.is_dashboard(name):
populate_worksheet(worksheet, 'dashboard', 'CodaLab Dashboard')
if spec_util.is_public_home(name):
populate_worksheet(worksheet, 'home', 'Public Home')
return worksheet.uuid
def ensure_unused_worksheet_name(name):
"""
Ensure worksheet names are unique.
Note: for simplicity, we are ensuring uniqueness across the system, even on
worksheet names that the user may not have access to.
"""
# If trying to set the name to a home worksheet, then it better be
# user's home worksheet.
if (
spec_util.is_home_worksheet(name)
and spec_util.home_worksheet(request.user.user_name) != name
):
raise UsageError(
'Cannot create %s because this is potentially the home worksheet of another user' % name
)
try:
canonicalize.get_worksheet_uuid(local.model, request.user, None, name)
raise UsageError('Worksheet with name %s already exists' % name)
except NotFoundError:
pass # all good!
@get('/bundles//contents/blob/' % spec_util.UUID_STR, name='fetch_bundle_contents_blob')
@get(
'/bundles//contents/blob/' % spec_util.UUID_STR,
name='fetch_bundle_contents_blob',
)
def _fetch_bundle_contents_blob(uuid, path=''):
"""
API to download the contents of a bundle or a subpath within a bundle.
For directories, this method always returns a tarred and gzipped archive of
the directory.
For files, if the request has an Accept-Encoding header containing gzip,
then the returned file is gzipped. Otherwise, the file is returned as-is.
HTTP Request headers:
- `Range: bytes=-`: fetch bytes from the range
def get_worksheet_uuid(self, base_worksheet_uuid, worksheet_spec):
"""
Return the uuid of the specified worksheet if it exists.
If not, create a new worksheet if the specified worksheet is home_worksheet or dashboard. Otherwise, throw an error.
"""
if worksheet_spec == '' or worksheet_spec == worksheet_util.HOME_WORKSHEET:
worksheet_spec = spec_util.home_worksheet(self._current_user_name())
worksheet_uuid = self.get_worksheet_uuid_or_none(base_worksheet_uuid, worksheet_spec)
if worksheet_uuid != None:
return worksheet_uuid
else:
if spec_util.is_home_worksheet(worksheet_spec) or spec_util.is_dashboard(worksheet_spec) or spec_util.is_public_home(worksheet_spec):
return self.new_worksheet(worksheet_spec)
else:
# let it throw the correct error message
return canonicalize.get_worksheet_uuid(self.model, base_worksheet_uuid, worksheet_spec)
# TODO(sckoo): switch over when ready (remember to include is_public_home)
@staticmethod
def resolve_worksheet_uuid(client, base_worksheet_uuid, worksheet_spec):
"""
Avoid making REST call if worksheet_spec is already a uuid.
"""
if spec_util.UUID_REGEX.match(worksheet_spec):
worksheet_uuid = worksheet_spec # Already uuid, don't need to look up specification
else:
worksheet_uuid = client.fetch_one('worksheets', params={
'base': base_worksheet_uuid,
'specs': [worksheet_spec],
})['uuid']
return worksheet_uuid
def validate(self, require_child_path=False):
"""
Validates that the dependency is well formed.
:param require_child_path: If True, make sure the child path is not empty
This is a needed condition for Run bundles, but not so for Make bundles
"""
spec_util.check_uuid(self.child_uuid)
spec_util.check_uuid(self.parent_uuid)
if not self.CHILD_PATH_REGEX.match(self.child_path):
raise UsageError(
'child_path must match %s, was %s'
% (self.CHILD_PATH_REGEX.pattern, self.child_path)
)
if require_child_path and len(self.child_path) == 0:
raise UsageError('child_path empty')
def ensure_unused_worksheet_name(name):
"""
Ensure worksheet names are unique.
Note: for simplicity, we are ensuring uniqueness across the system, even on
worksheet names that the user may not have access to.
"""
# If trying to set the name to a home worksheet, then it better be
# user's home worksheet.
if (
spec_util.is_home_worksheet(name)
and spec_util.home_worksheet(request.user.user_name) != name
):
raise UsageError(
'Cannot create %s because this is potentially the home worksheet of another user' % name
)
try:
canonicalize.get_worksheet_uuid(local.model, request.user, None, name)
raise UsageError('Worksheet with name %s already exists' % name)
except NotFoundError:
pass # all good!
def validate(self):
'''
Check a number of basic conditions that would indicate serious errors if
they do not hold. Right now, validation only checks this worksheet's uuid
and its name.
'''
spec_util.check_uuid(self.uuid)
spec_util.check_name(self.name)