Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Python 2 activities before they launch #992

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/sugar-launch
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ if args.debug:
cmd_args.extend(act_args)

os.chdir(str(activity.get_path()))
os.execvpe(cmd_args[0], cmd_args, activityfactory.get_environment(activity))
os.execvpe(cmd_args[0], cmd_args, activityfactory.get_environment(activity))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change removed newline at end of file. Did you review your change before commit?

48 changes: 43 additions & 5 deletions src/jarabe/journal/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import hashlib
from gettext import gettext as _
import re

from gi.repository import Gio
from gi.repository import Gtk
Expand Down Expand Up @@ -258,20 +259,58 @@ def ready_callback(metadata, source, destination):
model.copy(metadata, '/', ready_callback=ready_callback)


def is_python2_activity(bundle):
# Path to the main activity file
activity_file_path = os.path.join(bundle.get_path(), 'activity.py')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many activities do not have an activity.py file.


if not os.path.exists(activity_file_path):
return False # No activity file found

with open(activity_file_path, 'r') as f:
content = f.read()

# Check for Python 2 specific syntax patterns
python2_patterns = [
r'print\s+[^()]*$', # Print statement without parentheses
r'except\s+.*,\s*', # Old exception syntax
r'raw_input\s*\(', # raw_input function (Python 2)
r'long\s+', # long type (Python 2)
r'iteritems\s*\(', # iteritems method (Python 2)
r'xrange\s*\(', # xrange function (Python 2)
r'__future__\s+import\s+print_function', # Future import for print function
r'from\s+ConfigParser', # ConfigParser module (Python 2)
r'from\s+urlparse', # urlparse module (Python 2)
r'from\s+StringIO', # StringIO module (Python 2)
r'from\s+collections\s+import\s+OrderedDict', # OrderedDict import (Python 2)
]

# Check for any Python 2 patterns in the content
for pattern in python2_patterns:
if re.search(pattern, content):
return True # Detected Python 2 syntax

return False # Default to Python 3


def launch(bundle, activity_id=None, object_id=None, uri=None, color=None,
invited=False, alert_window=None):

bundle_id = bundle.get_bundle_id()

# Check if the activity is a Python 2 activity
if is_python2_activity(bundle):
logging.warning('Attempted to launch a Python 2 activity: %s', bundle_id)
# Emit a failure notification
shell.get_model().notify_launch_failed(activity_id)
return

if activity_id is None or not activity_id:
activity_id = activityfactory.create_activity_id()

logging.debug('launch bundle_id=%s activity_id=%s object_id=%s uri=%s',
bundle.get_bundle_id(), activity_id, object_id, uri)

if isinstance(bundle, ContentBundle):
# Content bundles are a special case: we treat them as launching
# Browse with a specific URI.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this comment?

uri = bundle.get_start_uri()
activities = get_activities_for_mime('text/html')
if len(activities) == 0:
Expand All @@ -297,7 +336,6 @@ def launch(bundle, activity_id=None, object_id=None, uri=None, color=None,

if not shell_model.can_launch_activity_instance(bundle):
if alert_window is None:
from jarabe.desktop import homewindow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this import?

alert_window = homewindow.get_instance()
if alert_window is not None:
alerts.show_multiple_instance_alert(
Expand Down Expand Up @@ -429,4 +467,4 @@ def get_mount_color(mount):
color = XoColor('%s,%s' %
(colors[index][0],
colors[index][1]))
return color
return color
amannaik247 marked this conversation as resolved.
Show resolved Hide resolved