<commit_before># Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'variables': {
    'chromium_code': 1,
  },
  'includes': [
    '../../../build/common.gypi',
  ],
  'targets': [
    {
      'target_name': 'test_shell_common',
      'type': 'static_library',
      'dependencies': [
        '../../../base/base.gyp:base',
        '../../../base/base.gyp:base_gfx',
        '../../../net/net.gyp:net',
        '../../../skia/skia.gyp:skia',
        '../../../testing/gtest.gyp:gtest',
        '../../../third_party/npapi/npapi.gyp:npapi',
        '../../webkit.gyp:glue',
        '../../webkit.gyp:webkit',
      ],
      'sources': [
        'mac/DumpRenderTreePasteboard.h',
        'mac/DumpRenderTreePasteboard.m',
        'mac/test_shell_webview.h',
        'mac/test_shell_webview.mm',
        'mac/test_webview_delegate.mm',
        'mac/webview_host.mm',
        'mac/webwidget_host.mm',
        'drag_delegate.cc',
        'drag_delegate.h',
        'drop_delegate.cc',
        'drop_delegate.h',
        'event_sending_controller.cc',
        'event_sending_controller.h',
        'foreground_helper.h',
        'layout_test_controller.cc',
        'layout_test_controller.h',
        'mock_webclipboard_impl.cc',
        'mock_webclipboard_impl.h',
        'resource.h',
        'simple_resource_loader_bridge.cc',
        'simple_resource_loader_bridge.h',
        'test_navigation_controller.cc',
        'test_navigation_controller.h',
        'test_shell.cc',
        'test_shell.h',
        'test_shell_gtk.cc',
        'test_shell_mac.mm',
        'test_shell_platform_delegate.h',
        'test_shell_platform_delegate_gtk.cc',
        'test_shell_platform_delegate_mac.mm',
        'test_shell_platform_delegate_win.cc',
        'test_shell_request_context.cc',
        'test_shell_request_context.h',
        'test_shell_switches.cc',
        'test_shell_switches.h',
        'test_shell_win.cc',
        'test_shell_webkit_init.h',
        'test_webview_delegate.cc',
        'test_webview_delegate.h',
        'test_webview_delegate_gtk.cc',
        'test_webview_delegate_win.cc',
        'test_webworker_helper.cc',
        'test_webworker_helper.h',
        'text_input_controller.cc',
        'text_input_controller.h',
        'webview_host.h',
        'webview_host_gtk.cc',
        'webview_host_win.cc',
        'webwidget_host.h',
        'webwidget_host_gtk.cc',
        'webwidget_host_win.cc',
      ],
      'export_dependent_settings': [
        '../../../base/base.gyp:base',
        '../../../net/net.gyp:net',
        '../../webkit.gyp:glue',
        '../../webkit.gyp:webkit',
      ],
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            'test_shell_resources',
            'npapi_layout_test_plugin',
            'npapi_test_plugin',
            '../../../build/linux/system.gyp:gtk',
          ],
          # for:  test_shell_gtk.cc
          'cflags': ['-Wno-multichar'],
        }, { # else: OS!=linux
          'sources/': [
            ['exclude', '_gtk\\.cc$']
          ],
        }],
        ['OS!="mac"', {
          'sources/': [
            ['exclude', 'mac/[^/]*\\.(cc|mm?)$'],
            ['exclude', '_mac\\.(cc|mm?)$'],
          ]
        }],
        ['OS=="win"', {
          'msvs_disabled_warnings': [ 4800 ],
          'link_settings': {
            'libraries': [
              '-lcomctl32.lib',
            ],
          },
          'dependencies': [
            '../../../breakpad/breakpad.gyp:breakpad_handler',
            '../../default_plugin/default_plugin.gyp:default_plugin',
          ],
        }, {  # else: OS!=win
          'sources/': [
            ['exclude', '_win\\.cc$']
          ],
          'sources!': [
            'drag_delegate.cc',
            'drop_delegate.cc',
          ],
        }],
      ],
    },
    {
      'target_name': 'test_shell',
      'type': 'executable',
      'mac_bundle': 1,
      'dependencies': [
        'test_shell_common',
      ],
      'sources': [
        'test_shell_main.cc',
      ],
      'mac_bundle_resources': [
        '../../data/test_shell/',
        'mac/English.lproj/InfoPlist.strings',
        'mac/English.lproj/MainMenu.nib',
        'mac/Info.plist',
        'mac/test_shell.icns',
        'resources/AHEM____.TTF',
      ],
      'mac_bundle_resources!': [
        # TODO(mark): Come up with a fancier way to do this (mac_info_plist?)
        # that automatically sets the correct INFOPLIST_FILE setting and adds
        # the file to a source group.
        'mac/Info.plist',
      ],
      'xcode_settings': {
        'INFOPLIST_FILE': 'mac/Info.plist',
      },
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            '../../../build/linux/system.gyp:gtk',
            '../../../net/net.gyp:net_resources',
            '../../webkit.gyp:glue', # for webkit_{resources,strings_en-US}.pak
            'test_shell_resources',
          ],
          'actions': [
            {
              'action_name': 'test_shell_repack',
              'inputs': [
                '<(SHARED_INTERMEDIATE_DIR)/net/net_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/test_shell_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_en-US.pak',
              ],
              'outputs': [
                '<(INTERMEDIATE_DIR)/repack/test_shell.pak',
              ],
              'action': ['python', '../../../tools/data_pack/repack.py', '<@(_outputs)', '<@(_inputs)'],
            },
          ],
          'copies': [
            {
              'destination': '<(PRODUCT_DIR)',
              'files': ['<(INTERMEDIATE_DIR)/repack/test_shell.pak'],
            },
          ],
        }],
        ['OS=="mac"', {
          'product_name': 'TestShell',
          'variables': {
            'repack_path': '../../../tools/data_pack/repack.py',
          },
          'actions': [
            {
              # TODO(mark): Make this work with more languages than the
              # hardcoded en-US.
              'action_name': 'repack_locale',
              'variables': {
                'pak_inputs': [
                  '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_en-US.pak',
                ],
              },
              'inputs': [
                '<(repack_path)',
                '<@(pak_inputs)',
              ],
              'outputs': [
                '<(INTERMEDIATE_DIR)/repack/test_shell.pak',
              ],
              'action': ['python', '<(repack_path)', '<@(_outputs)', '<@(pak_inputs)'],
              'process_outputs_as_mac_bundle_resources': 1,
            },
          ]
        }],
      ],
    },
    {
      'target_name': 'test_shell_tests',
      'type': 'executable',
      'dependencies': [
        'test_shell_common',
        '../../../skia/skia.gyp:skia',
        '../../../testing/gtest.gyp:gtest',
      ],
      'sources': [
        '../../../skia/ext/convolver_unittest.cc',
        '../../../skia/ext/platform_canvas_unittest.cc',
        '../../../skia/ext/vector_canvas_unittest.cc',
        '../../glue/bookmarklet_unittest.cc',
        '../../glue/context_menu_unittest.cc',
        '../../glue/cpp_bound_class_unittest.cc',
        '../../glue/cpp_variant_unittest.cc',
        '../../glue/devtools/dom_agent_unittest.cc',
        '../../glue/devtools/devtools_rpc_unittest.cc',
        '../../glue/dom_operations_unittest.cc',
        '../../glue/dom_serializer_unittest.cc',
        '../../glue/glue_serialize_unittest.cc',
        '../../glue/iframe_redirect_unittest.cc',
        '../../glue/mimetype_unittest.cc',
        '../../glue/multipart_response_delegate_unittest.cc',
        '../../glue/password_autocomplete_listener_unittest.cc',
        '../../glue/regular_expression_unittest.cc',
        '../../glue/resource_fetcher_unittest.cc',
        '../../glue/unittest_test_server.h',
        '../../glue/webframe_unittest.cc',
        '../../glue/webplugin_impl_unittest.cc',
        '../webcore_unit_tests/BMPImageDecoder_unittest.cpp',
        '../webcore_unit_tests/GKURL_unittest.cpp',
        '../webcore_unit_tests/ICOImageDecoder_unittest.cpp',
        '../webcore_unit_tests/UniscribeHelper_unittest.cpp',
        '../webcore_unit_tests/XBMImageDecoder_unittest.cpp',
        'image_decoder_unittest.cc',
        'image_decoder_unittest.h',
        'keyboard_unittest.cc',
        'layout_test_controller_unittest.cc',
        'node_leak_test.cc',
        'plugin_tests.cc',
        'run_all_tests.cc',
        'test_shell_test.cc',
        'test_shell_test.h',
        'text_input_controller_unittest.cc',
      ],
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            '../../../build/linux/system.gyp:gtk',
          ],
          'sources!': [
             # TODO(port)
            '../../../skia/ext/platform_canvas_unittest.cc',
            '../../glue/webplugin_impl_unittest.cc',
          ],
        }],
        ['OS=="mac"', {
          # mac tests load the resources from the built test_shell beside the
          # test
          'dependencies': ['test_shell'],
        }],
        ['OS=="win"', {
          'msvs_disabled_warnings': [ 4800 ],
        }, {  # else: OS!=win
          'sources!': [
            '../../../skia/ext/vector_canvas_unittest.cc',
            '../webcore_unit_tests/UniscribeHelper_unittest.cpp',
            'plugin_tests.cc'
          ],
        }],
      ],
    },
  ],
  'conditions': [
    ['OS=="linux"', {
      'targets': [
        {
          'target_name': 'test_shell_resources',
          'type': 'none',
          'sources': [
            'test_shell_resources.grd',
          ],
          # This was orignally in grit_resources.rules
          # NOTE: this version doesn't mimic the Properties specified there.
          'rules': [
            {
              'rule_name': 'grit',
              'extension': 'grd',
              'inputs': [
                '../../../tools/grit/grit.py',
              ],
              'outputs': [
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/grit/<(RULE_INPUT_ROOT).h',
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/<(RULE_INPUT_ROOT).pak',
              ],
              'action':
                ['python', '<@(_inputs)', '-i', '<(RULE_INPUT_PATH)', 'build', '-o', '<(SHARED_INTERMEDIATE_DIR)/test_shell'],
              'message': 'Generating resources from <(RULE_INPUT_PATH)',
            },
          ],
          'direct_dependent_settings': {
            'include_dirs': [
              '<(SHARED_INTERMEDIATE_DIR)/test_shell',
            ],
          },
        },
      ],
    }],
    # TODO:  change this condition to 'OS!="mac"'
    # when Windows is ready for the plugins, too.
    ['OS=="linux"', {
      'targets': [
        {
          'target_name': 'npapi_test_plugin',
          'type': 'loadable_module',
          'product_dir': '<(PRODUCT_DIR)/plugins',
          'dependencies': [
            '../../../base/base.gyp:base',
            '../../../third_party/icu38/icu38.gyp:icuuc',
            '../../../third_party/npapi/npapi.gyp:npapi',
          ],
          'sources': [
            '../../glue/plugins/test/npapi_constants.cc',
            '../../glue/plugins/test/npapi_test.cc',
            '../../glue/plugins/test/plugin_arguments_test.cc',
            '../../glue/plugins/test/plugin_client.cc',
            '../../glue/plugins/test/plugin_delete_plugin_in_stream_test.cc',
            '../../glue/plugins/test/plugin_execute_script_delete_test.cc',
            '../../glue/plugins/test/plugin_get_javascript_url_test.cc',
            '../../glue/plugins/test/plugin_geturl_test.cc',
            '../../glue/plugins/test/plugin_javascript_open_popup.cc',
            '../../glue/plugins/test/plugin_new_fails_test.cc',
            '../../glue/plugins/test/plugin_npobject_lifetime_test.cc',
            '../../glue/plugins/test/plugin_npobject_proxy_test.cc',
            '../../glue/plugins/test/plugin_test.cc',
            '../../glue/plugins/test/plugin_window_size_test.cc',
          ],
          'include_dirs': [
            '../../..',
          ],
          'conditions': [
            ['OS=="linux"', {
              'sources!': [
                # TODO(port):  Port these.
                # plugin_client.cc includes not ported headers.
                # plugin_npobject_lifetime_test.cc has win32-isms
                #   (HWND, CALLBACK).
                # plugin_window_size_test.cc has w32-isms including HWND.
                '../../glue/plugins/test/plugin_execute_script_delete_test.cc',
                '../../glue/plugins/test/plugin_javascript_open_popup.cc',
                '../../glue/plugins/test/plugin_client.cc',
                '../../glue/plugins/test/plugin_npobject_lifetime_test.cc',
                '../../glue/plugins/test/plugin_window_size_test.cc',
              ],
            }],
          ],
        },
        {
          'target_name': 'npapi_layout_test_plugin',
          'type': 'loadable_module',
          'product_dir': '<(PRODUCT_DIR)/plugins',
          'sources': [
            '../npapi_layout_test_plugin/main.cpp',
            '../npapi_layout_test_plugin/PluginObject.cpp',
            '../npapi_layout_test_plugin/TestObject.cpp',
          ],
          'include_dirs': [
            '../../..',
          ],
          'dependencies': [
            '../../../third_party/npapi/npapi.gyp:npapi',
            '../../webkit.gyp:wtf',
          ],
        },
      ],
    }],
  ],
}
<commit_msg>Fresh build problem:  make sure test_shell is built and installed when we ask for test_shell_tests. Review URL: http://codereview.chromium.org/56161<commit_after># Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
  'variables': {
    'chromium_code': 1,
  },
  'includes': [
    '../../../build/common.gypi',
  ],
  'targets': [
    {
      'target_name': 'test_shell_common',
      'type': 'static_library',
      'dependencies': [
        '../../../base/base.gyp:base',
        '../../../base/base.gyp:base_gfx',
        '../../../net/net.gyp:net',
        '../../../skia/skia.gyp:skia',
        '../../../testing/gtest.gyp:gtest',
        '../../../third_party/npapi/npapi.gyp:npapi',
        '../../webkit.gyp:glue',
        '../../webkit.gyp:webkit',
      ],
      'sources': [
        'mac/DumpRenderTreePasteboard.h',
        'mac/DumpRenderTreePasteboard.m',
        'mac/test_shell_webview.h',
        'mac/test_shell_webview.mm',
        'mac/test_webview_delegate.mm',
        'mac/webview_host.mm',
        'mac/webwidget_host.mm',
        'drag_delegate.cc',
        'drag_delegate.h',
        'drop_delegate.cc',
        'drop_delegate.h',
        'event_sending_controller.cc',
        'event_sending_controller.h',
        'foreground_helper.h',
        'layout_test_controller.cc',
        'layout_test_controller.h',
        'mock_webclipboard_impl.cc',
        'mock_webclipboard_impl.h',
        'resource.h',
        'simple_resource_loader_bridge.cc',
        'simple_resource_loader_bridge.h',
        'test_navigation_controller.cc',
        'test_navigation_controller.h',
        'test_shell.cc',
        'test_shell.h',
        'test_shell_gtk.cc',
        'test_shell_mac.mm',
        'test_shell_platform_delegate.h',
        'test_shell_platform_delegate_gtk.cc',
        'test_shell_platform_delegate_mac.mm',
        'test_shell_platform_delegate_win.cc',
        'test_shell_request_context.cc',
        'test_shell_request_context.h',
        'test_shell_switches.cc',
        'test_shell_switches.h',
        'test_shell_win.cc',
        'test_shell_webkit_init.h',
        'test_webview_delegate.cc',
        'test_webview_delegate.h',
        'test_webview_delegate_gtk.cc',
        'test_webview_delegate_win.cc',
        'test_webworker_helper.cc',
        'test_webworker_helper.h',
        'text_input_controller.cc',
        'text_input_controller.h',
        'webview_host.h',
        'webview_host_gtk.cc',
        'webview_host_win.cc',
        'webwidget_host.h',
        'webwidget_host_gtk.cc',
        'webwidget_host_win.cc',
      ],
      'export_dependent_settings': [
        '../../../base/base.gyp:base',
        '../../../net/net.gyp:net',
        '../../webkit.gyp:glue',
        '../../webkit.gyp:webkit',
      ],
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            'test_shell_resources',
            'npapi_layout_test_plugin',
            'npapi_test_plugin',
            '../../../build/linux/system.gyp:gtk',
          ],
          # for:  test_shell_gtk.cc
          'cflags': ['-Wno-multichar'],
        }, { # else: OS!=linux
          'sources/': [
            ['exclude', '_gtk\\.cc$']
          ],
        }],
        ['OS!="mac"', {
          'sources/': [
            ['exclude', 'mac/[^/]*\\.(cc|mm?)$'],
            ['exclude', '_mac\\.(cc|mm?)$'],
          ]
        }],
        ['OS=="win"', {
          'msvs_disabled_warnings': [ 4800 ],
          'link_settings': {
            'libraries': [
              '-lcomctl32.lib',
            ],
          },
          'dependencies': [
            '../../../breakpad/breakpad.gyp:breakpad_handler',
            '../../default_plugin/default_plugin.gyp:default_plugin',
          ],
        }, {  # else: OS!=win
          'sources/': [
            ['exclude', '_win\\.cc$']
          ],
          'sources!': [
            'drag_delegate.cc',
            'drop_delegate.cc',
          ],
        }],
      ],
    },
    {
      'target_name': 'test_shell',
      'type': 'executable',
      'mac_bundle': 1,
      'dependencies': [
        'test_shell_common',
      ],
      'sources': [
        'test_shell_main.cc',
      ],
      'mac_bundle_resources': [
        '../../data/test_shell/',
        'mac/English.lproj/InfoPlist.strings',
        'mac/English.lproj/MainMenu.nib',
        'mac/Info.plist',
        'mac/test_shell.icns',
        'resources/AHEM____.TTF',
      ],
      'mac_bundle_resources!': [
        # TODO(mark): Come up with a fancier way to do this (mac_info_plist?)
        # that automatically sets the correct INFOPLIST_FILE setting and adds
        # the file to a source group.
        'mac/Info.plist',
      ],
      'xcode_settings': {
        'INFOPLIST_FILE': 'mac/Info.plist',
      },
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            '../../../build/linux/system.gyp:gtk',
            '../../../net/net.gyp:net_resources',
            '../../webkit.gyp:glue', # for webkit_{resources,strings_en-US}.pak
            'test_shell_resources',
          ],
          'actions': [
            {
              'action_name': 'test_shell_repack',
              'inputs': [
                '<(SHARED_INTERMEDIATE_DIR)/net/net_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/test_shell_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_resources.pak',
                '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_en-US.pak',
              ],
              'outputs': [
                '<(INTERMEDIATE_DIR)/repack/test_shell.pak',
              ],
              'action': ['python', '../../../tools/data_pack/repack.py', '<@(_outputs)', '<@(_inputs)'],
            },
          ],
          'copies': [
            {
              'destination': '<(PRODUCT_DIR)',
              'files': ['<(INTERMEDIATE_DIR)/repack/test_shell.pak'],
            },
          ],
        }],
        ['OS=="mac"', {
          'product_name': 'TestShell',
          'variables': {
            'repack_path': '../../../tools/data_pack/repack.py',
          },
          'actions': [
            {
              # TODO(mark): Make this work with more languages than the
              # hardcoded en-US.
              'action_name': 'repack_locale',
              'variables': {
                'pak_inputs': [
                  '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_en-US.pak',
                ],
              },
              'inputs': [
                '<(repack_path)',
                '<@(pak_inputs)',
              ],
              'outputs': [
                '<(INTERMEDIATE_DIR)/repack/test_shell.pak',
              ],
              'action': ['python', '<(repack_path)', '<@(_outputs)', '<@(pak_inputs)'],
              'process_outputs_as_mac_bundle_resources': 1,
            },
          ]
        }],
      ],
    },
    {
      'target_name': 'test_shell_tests',
      'type': 'executable',
      'dependencies': [
        'test_shell_common',
        '../../../skia/skia.gyp:skia',
        '../../../testing/gtest.gyp:gtest',
      ],
      'sources': [
        '../../../skia/ext/convolver_unittest.cc',
        '../../../skia/ext/platform_canvas_unittest.cc',
        '../../../skia/ext/vector_canvas_unittest.cc',
        '../../glue/bookmarklet_unittest.cc',
        '../../glue/context_menu_unittest.cc',
        '../../glue/cpp_bound_class_unittest.cc',
        '../../glue/cpp_variant_unittest.cc',
        '../../glue/devtools/dom_agent_unittest.cc',
        '../../glue/devtools/devtools_rpc_unittest.cc',
        '../../glue/dom_operations_unittest.cc',
        '../../glue/dom_serializer_unittest.cc',
        '../../glue/glue_serialize_unittest.cc',
        '../../glue/iframe_redirect_unittest.cc',
        '../../glue/mimetype_unittest.cc',
        '../../glue/multipart_response_delegate_unittest.cc',
        '../../glue/password_autocomplete_listener_unittest.cc',
        '../../glue/regular_expression_unittest.cc',
        '../../glue/resource_fetcher_unittest.cc',
        '../../glue/unittest_test_server.h',
        '../../glue/webframe_unittest.cc',
        '../../glue/webplugin_impl_unittest.cc',
        '../webcore_unit_tests/BMPImageDecoder_unittest.cpp',
        '../webcore_unit_tests/GKURL_unittest.cpp',
        '../webcore_unit_tests/ICOImageDecoder_unittest.cpp',
        '../webcore_unit_tests/UniscribeHelper_unittest.cpp',
        '../webcore_unit_tests/XBMImageDecoder_unittest.cpp',
        'image_decoder_unittest.cc',
        'image_decoder_unittest.h',
        'keyboard_unittest.cc',
        'layout_test_controller_unittest.cc',
        'node_leak_test.cc',
        'plugin_tests.cc',
        'run_all_tests.cc',
        'test_shell_test.cc',
        'test_shell_test.h',
        'text_input_controller_unittest.cc',
      ],
      'conditions': [
        ['OS=="linux"', {
          'dependencies': [
            # Linux tests use the built test_shell beside the test
            'test_shell',
            '../../../build/linux/system.gyp:gtk',
          ],
          'sources!': [
             # TODO(port)
            '../../../skia/ext/platform_canvas_unittest.cc',
            '../../glue/webplugin_impl_unittest.cc',
          ],
        }],
        ['OS=="mac"', {
          # mac tests load the resources from the built test_shell beside the
          # test
          'dependencies': ['test_shell'],
        }],
        ['OS=="win"', {
          'msvs_disabled_warnings': [ 4800 ],
        }, {  # else: OS!=win
          'sources!': [
            '../../../skia/ext/vector_canvas_unittest.cc',
            '../webcore_unit_tests/UniscribeHelper_unittest.cpp',
            'plugin_tests.cc'
          ],
        }],
      ],
    },
  ],
  'conditions': [
    ['OS=="linux"', {
      'targets': [
        {
          'target_name': 'test_shell_resources',
          'type': 'none',
          'sources': [
            'test_shell_resources.grd',
          ],
          # This was orignally in grit_resources.rules
          # NOTE: this version doesn't mimic the Properties specified there.
          'rules': [
            {
              'rule_name': 'grit',
              'extension': 'grd',
              'inputs': [
                '../../../tools/grit/grit.py',
              ],
              'outputs': [
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/grit/<(RULE_INPUT_ROOT).h',
                '<(SHARED_INTERMEDIATE_DIR)/test_shell/<(RULE_INPUT_ROOT).pak',
              ],
              'action':
                ['python', '<@(_inputs)', '-i', '<(RULE_INPUT_PATH)', 'build', '-o', '<(SHARED_INTERMEDIATE_DIR)/test_shell'],
              'message': 'Generating resources from <(RULE_INPUT_PATH)',
            },
          ],
          'direct_dependent_settings': {
            'include_dirs': [
              '<(SHARED_INTERMEDIATE_DIR)/test_shell',
            ],
          },
        },
      ],
    }],
    # TODO:  change this condition to 'OS!="mac"'
    # when Windows is ready for the plugins, too.
    ['OS=="linux"', {
      'targets': [
        {
          'target_name': 'npapi_test_plugin',
          'type': 'loadable_module',
          'product_dir': '<(PRODUCT_DIR)/plugins',
          'dependencies': [
            '../../../base/base.gyp:base',
            '../../../third_party/icu38/icu38.gyp:icuuc',
            '../../../third_party/npapi/npapi.gyp:npapi',
          ],
          'sources': [
            '../../glue/plugins/test/npapi_constants.cc',
            '../../glue/plugins/test/npapi_test.cc',
            '../../glue/plugins/test/plugin_arguments_test.cc',
            '../../glue/plugins/test/plugin_client.cc',
            '../../glue/plugins/test/plugin_delete_plugin_in_stream_test.cc',
            '../../glue/plugins/test/plugin_execute_script_delete_test.cc',
            '../../glue/plugins/test/plugin_get_javascript_url_test.cc',
            '../../glue/plugins/test/plugin_geturl_test.cc',
            '../../glue/plugins/test/plugin_javascript_open_popup.cc',
            '../../glue/plugins/test/plugin_new_fails_test.cc',
            '../../glue/plugins/test/plugin_npobject_lifetime_test.cc',
            '../../glue/plugins/test/plugin_npobject_proxy_test.cc',
            '../../glue/plugins/test/plugin_test.cc',
            '../../glue/plugins/test/plugin_window_size_test.cc',
          ],
          'include_dirs': [
            '../../..',
          ],
          'conditions': [
            ['OS=="linux"', {
              'sources!': [
                # TODO(port):  Port these.
                # plugin_client.cc includes not ported headers.
                # plugin_npobject_lifetime_test.cc has win32-isms
                #   (HWND, CALLBACK).
                # plugin_window_size_test.cc has w32-isms including HWND.
                '../../glue/plugins/test/plugin_execute_script_delete_test.cc',
                '../../glue/plugins/test/plugin_javascript_open_popup.cc',
                '../../glue/plugins/test/plugin_client.cc',
                '../../glue/plugins/test/plugin_npobject_lifetime_test.cc',
                '../../glue/plugins/test/plugin_window_size_test.cc',
              ],
            }],
          ],
        },
        {
          'target_name': 'npapi_layout_test_plugin',
          'type': 'loadable_module',
          'product_dir': '<(PRODUCT_DIR)/plugins',
          'sources': [
            '../npapi_layout_test_plugin/main.cpp',
            '../npapi_layout_test_plugin/PluginObject.cpp',
            '../npapi_layout_test_plugin/TestObject.cpp',
          ],
          'include_dirs': [
            '../../..',
          ],
          'dependencies': [
            '../../../third_party/npapi/npapi.gyp:npapi',
            '../../webkit.gyp:wtf',
          ],
        },
      ],
    }],
  ],
}
<|endoftext|>


<commit_before>from uuid import uuid4

class PrimaryDiagnostic(object):
    def __init__(self, config):
        """
            Setup class attributes
        """
        self.inputs = {
            'coupled_diag_path': '',
            'test_casename': '',
            'test_native_res': '',
            'test_archive_dir': '',
            'test_begin_yr_climo': '',
            'test_end_yr_climo': '',
            'test_begin_yr_ts': '',
            'test_end_yr_ts': '',
            'ref_case': '',
            'ref_archive_dir': '',
        }
        self.outputs = {}
        self.uuid = uuid4().hex
        print "init"

    def prevalidate(self, config):
        """
            Iterate over given config dictionary making sure all the inputs are set
            and rejecting any inputs that arent in the input dict
        """
        print 'prevalidate'

    def postvalidate(self):
        """
            Check that what the job was supposed to do actually happened
        """
        print 'postvalidate'

    def execute(self):
        """
            Perform the actual work
        """
        print 'execute'
<commit_msg>Begins fleshing out the primary diags job<commit_after>import os

from uuid import uuid4
from util import render
from pprint import pformat

class PrimaryDiagnostic(object):
    def __init__(self, config):
        """
        Setup class attributes
        """
        self.inputs = {
            'coupled_project_dir': '',
            'test_casename': '',
            'test_native_res': '',
            'test_archive_dir': '',
            'test_begin_yr_climo': '',
            'test_end_yr_climo': '',
            'test_begin_yr_ts': '',
            'test_end_yr_ts': '',
            'ref_case': '',
            'ref_archive_dir': '',
            'mpas_meshfile': '',
            'mpas_remapfile': '',
            'pop_remapfile': '',
            'remap_files_dir': '',
            'GPCP_regrid_wgt_file': '',
            'CERES_EBAF_regrid_wgt_file': '',
            'ERS_regrid_wgt_file': '',
            'coupled_home_directory': '',
            'coupled_template_path': '',
            'rendered_output_path': '',
            'obs_ocndir': '',
            'obs_seaicedir': '',
            'obs_sstdir': '',
            'obs_iceareaNH': '',
            'obs_iceareaSH': '',
            'obs_icevolNH': '',
            'obs_icevolSH': ''
        }
        self.config = {}
        self.status = 'unvalidated'
        self.type = 'primary_diag'
        self.outputs = {}
        self.uuid = uuid4().hex
        self.job_id = 0
        self.depends_on = []
        self.prevalidate(config)
        print "init"

    def __str__(self):
        return pformat({
            'type': self.type,
            'config': self.config,
            'status': self.status,
            'depends_on': self.depends_on,
            'uuid': self.uuid,
            'job_id': self.job_id
        }, indent=4)

    def prevalidate(self, config):
        """
        Iterate over given config dictionary making sure all the inputs are set
        and rejecting any inputs that arent in the input dict
        """
        self.config = config
        self.depends_on = config.get('depends_on')
        self.status = 'valid'

    def postvalidate(self):
        """
        Check that what the job was supposed to do actually happened
        """
        print 'postvalidate'

    def execute(self):
        """
        Perform the actual work
        """
        render(
            variables=self.config,
            input_path=self.config.get('coupled_template_path'),
            output_path=self.config.get('rendered_output_path'),
            delimiter='%%')
        return
        #os.chdir(self.config.get('coupled_home_directory'))

<|endoftext|>


<commit_before>import logging
import os

from .cache import HashCache, CacheMissException
from .ffprobe import FFprobeInfoCommand
from .field_mode_solver import FFprobeFieldModeSolver
from .factory import ffprobe_factory
from .exceptions import FFprobeProcessException, MetadataCollectionException

class FFprobeMetadataResult:

    def __init__(self, input_url: str, info: dict, int_prog_solver: FFprobeFieldModeSolver):
        self._input_url = input_url
        self._info = info
        self._int_prog_solver = int_prog_solver
        self._av_streams_found = False
        self._v_streams_numbers = []
        self._a_streams_numbers = []
        self._field_mode = {}
        self._filename = ''
        self._filename_ext = ''

    def _find_av_streams(self):
        logging.debug('Searching for video and audio streams...')
        for n, s in enumerate(self._info['streams']):
            if s['codec_type'] == 'video':
                self._v_streams_numbers.append(n)
            elif s['codec_type'] == 'audio':
                self._a_streams_numbers.append(n)
        logging.debug('Found {} video stream(s) and {} audio stream(s)'.format(
            len(self._v_streams_numbers), len(self._a_streams_numbers)
        ))
        self._av_streams_found = True

    @property
    def v_streams(self) -> dict:
        if not self._av_streams_found:
            self._find_av_streams()
        return {self._info['streams'][n]['index']: self._info['streams'][n] for n in self._v_streams_numbers}

    @property
    def a_streams(self) -> dict:
        if not self._av_streams_found:
            self._find_av_streams()
        return {self._info['streams'][n]['index']: self._info['streams'][n] for n in self._a_streams_numbers}

    @property
    def format(self) -> dict:
        return self._info['format']

    @property
    def filename(self) -> str:
        if not self._filename:
            self._filename = os.path.splitext(self.filename_ext)[0]
        return self._filename

    @property
    def filename_ext(self) -> str:
        if not self._filename_ext:
            self._filename_ext = os.path.split(self._input_url)[1]
        return self._filename_ext

    def get_field_mode(self, stream_number: int) -> int:
        if stream_number not in self._field_mode:
            self._field_mode[stream_number] = self._int_prog_solver.solve(self._input_url, stream_number)
        return self._field_mode[stream_number]

    def __str__(self):
        return {
            'a_streams': self.a_streams,
            'filename': self.filename,
            'filename_ext': self.filename_ext,
            'format': self.format,
            'v_streams': self.v_streams,
        }

class FFprobeMetadataCollector:

    def __init__(self):
        logging.debug('Fetching FFprobeInfoCommand object...')
        self._ffprobe_info = ffprobe_factory.get_ffprobe_command(FFprobeInfoCommand)
        logging.debug('Fetching FFprobeFieldModeSolver object...')
        self._int_prog_solver = ffprobe_factory.get_ffprobe_field_mode_solver(FFprobeFieldModeSolver)
        self._cache = HashCache(10, logging.debug)

    def get_metadata(self, input_url: str) -> FFprobeMetadataResult:
        logging.debug('Trying to get file metadata from cache...')
        try:
            cached_value = self._cache.from_cache(input_url)
        except CacheMissException:
            pass
        else:
            return cached_value
        try:
            result = FFprobeMetadataResult(
                input_url,
                self._ffprobe_info.exec(input_url, show_programs=False),
                self._int_prog_solver
            )
        except FFprobeProcessException as e:
            raise MetadataCollectionException from e
        self._cache.to_cache(input_url, result)
        return result
<commit_msg>fix: __str___ returned dict<commit_after>import logging
import os

from .cache import HashCache, CacheMissException
from .ffprobe import FFprobeInfoCommand
from .field_mode_solver import FFprobeFieldModeSolver
from .factory import ffprobe_factory
from .exceptions import FFprobeProcessException, MetadataCollectionException

class FFprobeMetadataResult:

    def __init__(self, input_url: str, info: dict, int_prog_solver: FFprobeFieldModeSolver):
        self._input_url = input_url
        self._info = info
        self._int_prog_solver = int_prog_solver
        self._av_streams_found = False
        self._v_streams_numbers = []
        self._a_streams_numbers = []
        self._field_mode = {}
        self._filename = ''
        self._filename_ext = ''

    def _find_av_streams(self):
        logging.debug('Searching for video and audio streams...')
        for n, s in enumerate(self._info['streams']):
            if s['codec_type'] == 'video':
                self._v_streams_numbers.append(n)
            elif s['codec_type'] == 'audio':
                self._a_streams_numbers.append(n)
        logging.debug('Found {} video stream(s) and {} audio stream(s)'.format(
            len(self._v_streams_numbers), len(self._a_streams_numbers)
        ))
        self._av_streams_found = True

    @property
    def v_streams(self) -> dict:
        if not self._av_streams_found:
            self._find_av_streams()
        return {self._info['streams'][n]['index']: self._info['streams'][n] for n in self._v_streams_numbers}

    @property
    def a_streams(self) -> dict:
        if not self._av_streams_found:
            self._find_av_streams()
        return {self._info['streams'][n]['index']: self._info['streams'][n] for n in self._a_streams_numbers}

    @property
    def format(self) -> dict:
        return self._info['format']

    @property
    def filename(self) -> str:
        if not self._filename:
            self._filename = os.path.splitext(self.filename_ext)[0]
        return self._filename

    @property
    def filename_ext(self) -> str:
        if not self._filename_ext:
            self._filename_ext = os.path.split(self._input_url)[1]
        return self._filename_ext

    def get_field_mode(self, stream_number: int) -> int:
        if stream_number not in self._field_mode:
            self._field_mode[stream_number] = self._int_prog_solver.solve(self._input_url, stream_number)
        return self._field_mode[stream_number]

    def __str__(self):
        return str({
            'a_streams': self.a_streams,
            'filename': self.filename,
            'filename_ext': self.filename_ext,
            'format': self.format,
            'v_streams': self.v_streams,
        })

class FFprobeMetadataCollector:

    def __init__(self):
        logging.debug('Fetching FFprobeInfoCommand object...')
        self._ffprobe_info = ffprobe_factory.get_ffprobe_command(FFprobeInfoCommand)
        logging.debug('Fetching FFprobeFieldModeSolver object...')
        self._int_prog_solver = ffprobe_factory.get_ffprobe_field_mode_solver(FFprobeFieldModeSolver)
        self._cache = HashCache(10, logging.debug)

    def get_metadata(self, input_url: str) -> FFprobeMetadataResult:
        logging.debug('Trying to get file metadata from cache...')
        try:
            cached_value = self._cache.from_cache(input_url)
        except CacheMissException:
            pass
        else:
            return cached_value
        try:
            result = FFprobeMetadataResult(
                input_url,
                self._ffprobe_info.exec(input_url, show_programs=False),
                self._int_prog_solver
            )
        except FFprobeProcessException as e:
            raise MetadataCollectionException from e
        self._cache.to_cache(input_url, result)
        return result
<|endoftext|>


<commit_before>import numpy

from .stain_color_map import stain_color_map

def get_stain_vector(args, index):
    """Get the stain corresponding to args.stain_$index and
    args.stain_$index_vector.  If the former is not "custom", the
    latter must be None.

    """
    args = vars(args)
    stain = args['stain_' + str(index)]
    stain_vector = args['stain_' + str(index) + '_vector']
    if stain == 'custom':
        if stain_vector is None:
            raise ValueError('If "custom" is chosen for a stain, '
                             'a stain vector must be provided.')
        return stain_vector
    else:
        if stain_vector is None:
            return stain_color_map[stain]
        raise ValueError('Unless "custom" is chosen for a stain, '
                         'no stain vector may be provided.')

def get_stain_matrix(args):
    """Get the stain matrix corresponding to the args.stain_$index and
    args.stain_$index_vector arguments for values of index 1, 2, 3.
    Return a numpy array of column vectors.

    """
    return numpy.array([get_stain_vector(args, i) for i in 1, 2, 3]).T

__all__ = (
    'get_stain_vector',
)
<commit_msg>Enhance get_stain_matrix to take any desired number of vectors<commit_after>import numpy

from .stain_color_map import stain_color_map

def get_stain_vector(args, index):
    """Get the stain corresponding to args.stain_$index and
    args.stain_$index_vector.  If the former is not "custom", the
    latter must be None.

    """
    args = vars(args)
    stain = args['stain_' + str(index)]
    stain_vector = args['stain_' + str(index) + '_vector']
    if stain == 'custom':
        if stain_vector is None:
            raise ValueError('If "custom" is chosen for a stain, '
                             'a stain vector must be provided.')
        return stain_vector
    else:
        if stain_vector is None:
            return stain_color_map[stain]
        raise ValueError('Unless "custom" is chosen for a stain, '
                         'no stain vector may be provided.')

def get_stain_matrix(args, count=3):
    """Get the stain matrix corresponding to the args.stain_$index and
    args.stain_$index_vector arguments for values of index 1 to count.
    Return a numpy array of column vectors.

    """
    return numpy.array([get_stain_vector(args, i+1) for i in range(count)]).T

__all__ = (
    'get_stain_vector',
)
<|endoftext|>


<commit_before>from django.db import models

class Report(models.Model):
    device_id = models.CharField(max_length=50)
    timestamp = models.DateTimeField()
    created = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
        return '%s @%s' % (self.device_id,self.timestamp)

    def is_station(self):
        """ this function returns using iteration and not using filter().exists()
        since it is called after prefetch_related, and using this style
        does not force acceess to the DB """
        for wifi in self.get_wifi_set_all():
            if wifi.SSID == 'S-ISRAEL-RAILWAYS':
                return True
        return False

    def loc_ts_delta(self):
        if self.get_my_loc():
            return (self.timestamp - self.get_my_loc().timestamp).total_seconds()

    def get_timestamp_israel_time(self):
        #local_time_delta = datetime.timedelta(0,2*3600)
        #return self.timestamp + local_time_delta
        from common.ot_utils import get_localtime
        timestamp = get_localtime(self.timestamp)
        timestamp = timestamp.replace(microsecond=0)
        return timestamp

    def to_api_dict(self):
        result = dict()
        result['created'] = self.created.isoformat()
        result['timestamp'] = self.timestamp.isoformat()
        result['device_id'] = self.device_id
        result['id'] = self.id
        result['is_station'] = self.is_station()
        if self.my_loc:
            result['loc'] = self.my_loc.to_api_dict()
        return result

    def get_my_loc(self):
        if self.pk and hasattr(self, 'my_loc'):
            return self.my_loc
        elif not self.pk and hasattr(self, 'my_loc_mock'):
            return self.my_loc_mock
        else:
            return None

    def get_wifi_set_all(self):
        if self.pk:
            return self.wifi_set.all()
        else:
            return self.wifi_set_mock

class LocationInfo(models.Model):
    report = models.OneToOneField(Report,related_name='my_loc')
    accuracy = models.FloatField()
    lat = models.FloatField()
    lon = models.FloatField()
    provider = models.CharField(max_length=100)
    timestamp = models.DateTimeField()

    @property
    def accuracy_in_coords(self):
        from common.ot_utils import meter_distance_to_coord_distance
        return meter_distance_to_coord_distance(self.accuracy)

    def to_api_dict(self):
        return dict(lat=self.lat,
                    lon=self.lon,
                    id=self.id,
                    provider=self.provider,
                    accuracy=self.accuracy,
                    timestamp=self.timestamp.isoformat())

class SingleWifiReport(models.Model):
    report = models.ForeignKey(Report,related_name='wifi_set')
    SSID = models.CharField(max_length=50)
    frequency = models.FloatField()
    key = models.CharField(max_length=30)
    signal = models.IntegerField()
    def __unicode__(self):
        return self.SSID

class RealTimeStop(models.Model):
    tracker_id = models.CharField(max_length=40,db_index=True)
    trip = models.ForeignKey('gtfs.Trip')
    stop = models.ForeignKey('gtfs.Stop')
    arrival_time = models.DateTimeField()
    departure_time = models.DateTimeField(blank=True,null=True)
    class Meta:
        unique_together = (('tracker_id','trip','stop'),)

    def __unicode__(self):
        return '%s %s @%s' % (self.tracker,self.stop,self.arrival_time)

class AnalysisMarker(models.Model):
    label = models.CharField(max_length=30)
    text = models.TextField()
    lat = models.CharField(max_length=10)
    lon = models.CharField(max_length=10)

<commit_msg>run time model<commit_after>from django.db import models

class Report(models.Model):
    device_id = models.CharField(max_length=50)
    timestamp = models.DateTimeField()
    created = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
        return '%s @%s' % (self.device_id,self.timestamp)

    def is_station(self):
        """ this function returns using iteration and not using filter().exists()
        since it is called after prefetch_related, and using this style
        does not force acceess to the DB """
        for wifi in self.get_wifi_set_all():
            if wifi.SSID == 'S-ISRAEL-RAILWAYS':
                return True
        return False

    def loc_ts_delta(self):
        if self.get_my_loc():
            return (self.timestamp - self.get_my_loc().timestamp).total_seconds()

    def get_timestamp_israel_time(self):
        #local_time_delta = datetime.timedelta(0,2*3600)
        #return self.timestamp + local_time_delta
        from common.ot_utils import get_localtime
        timestamp = get_localtime(self.timestamp)
        timestamp = timestamp.replace(microsecond=0)
        return timestamp

    def to_api_dict(self):
        result = dict()
        result['created'] = self.created.isoformat()
        result['timestamp'] = self.timestamp.isoformat()
        result['device_id'] = self.device_id
        result['id'] = self.id
        result['is_station'] = self.is_station()
        if self.my_loc:
            result['loc'] = self.my_loc.to_api_dict()
        return result

    def get_my_loc(self):
        if self.pk and hasattr(self, 'my_loc'):
            return self.my_loc
        elif not self.pk and hasattr(self, 'my_loc_mock'):
            return self.my_loc_mock
        else:
            return None

    def get_wifi_set_all(self):
        if self.pk:
            return self.wifi_set.all()
        else:
            return self.wifi_set_mock

class LocationInfo(models.Model):
    report = models.OneToOneField(Report,related_name='my_loc')
    accuracy = models.FloatField()
    lat = models.FloatField()
    lon = models.FloatField()
    provider = models.CharField(max_length=100)
    timestamp = models.DateTimeField()

    @property
    def accuracy_in_coords(self):
        from common.ot_utils import meter_distance_to_coord_distance
        return meter_distance_to_coord_distance(self.accuracy)

    def to_api_dict(self):
        return dict(lat=self.lat,
                    lon=self.lon,
                    id=self.id,
                    provider=self.provider,
                    accuracy=self.accuracy,
                    timestamp=self.timestamp.isoformat())

class SingleWifiReport(models.Model):
    report = models.ForeignKey(Report,related_name='wifi_set')
    SSID = models.CharField(max_length=50)
    frequency = models.FloatField()
    key = models.CharField(max_length=30)
    signal = models.IntegerField()
    def __unicode__(self):
        return self.SSID

class RealTimeStop(models.Model):
    tracker_id = models.CharField(max_length=40,db_index=True)
    trip = models.ForeignKey('gtfs.Trip')
    stop = models.ForeignKey('gtfs.Stop')
    arrival_time = models.DateTimeField()
    departure_time = models.DateTimeField(blank=True,null=True)
    class Meta:
        unique_together = (('tracker_id','trip','stop'),)

    def __unicode__(self):
        return '%s %s @%s' % (self.tracker,self.stop,self.arrival_time)

<|endoftext|>


<commit_before>from UnitTest import UnitTest

class Stupid:
    pass

class Foo:

    def __init__(self, v):
        self._v = v

    def __nonzero__(self):
        return self._v>0

    def __len__(self):
        return 1

class Bar:

    def __init__(self, v):
        self._v = v

    def __len__(self):
        return self._v

class BoolTest(UnitTest):

    def testBaseTypes(self):
        # meta test first
        self.assertTrue(True)
        self.assertFalse(False)

        # booleans
        self.assertTrue(bool(True))
        self.assertFalse(bool(False))
        self.assertTrue(True == True)
        self.assertFalse(False == True)

        # ints
        self.assertTrue(bool(1))
        self.assertFalse(bool(0))
        self.assertTrue(1)
        self.assertFalse(0)

        # strings
        self.assertTrue(bool('a'))
        self.assertFalse(bool(''))
        self.assertTrue('a')
        self.assertFalse('')

    def testObjects(self):

        # objects
        self.assertTrue(bool(Stupid()))
        self.assertTrue(Stupid())

        # __nonzero__ has precedence
        self.assertFalse(bool(Foo(0)))
        self.assertTrue(bool(Foo(1)))
        self.assertFalse(Foo(0))
        self.assertTrue(Foo(1))

        # __len__ is used secondary
        self.assertFalse(bool(Bar(0)))
        self.assertTrue(bool(Bar(1)))
        self.assertFalse(Bar(0))
        self.assertTrue(Bar(1))

        # lists
        self.assertFalse(bool([]))
        self.assertTrue(bool([1]))
        self.assertFalse([])
        self.assertTrue([1])

        # dicts
        self.assertFalse(bool({}))
        self.assertTrue(bool({'x':1}))
        self.assertFalse({})
        self.assertTrue({'x':1})

    def testIfStatement(self):

        if({}):
            self.fail("Empty dict should not evaluate to True in If")
        else:
            self.assertTrue(True)
        if({1: 5}):
            self.assertTrue(True)
        else:
            self.fail("Non-empty dict should evaluate to True in If")
        if([]):
            self.fail("Empty dict should not evaluate to True in If")
        else:
            self.assertTrue(True)
        if([1]):
            self.assertTrue(True)
        else:
            self.fail("Non-empty lists should evaluate to True in If")
        if not []:
            self.assertTrue(True)
        else:
            self.fail("Non empty lists should not evaluate to True in If")
        if [1] and {}:
            self.fail("'[1] and {}' shoul evaluate to False")
        else:
            self.assertTrue(True)
        if [] or not {}:
            self.assertTrue(True)
        else:
            self.fail("'[] or not {}' shoul evaluate to True")
        if [] and not {}:
            self.fail("'[] and not {}' shoul evaluate to False")
        else:
            self.assertTrue(True)
        if 0 < 1 < 2:
            self.assertTrue(True)
        else:
            self.fail("if 0 < 1 < 2")
        i = [0, 2, 4]
        i = i.__iter__();
        if 0 < i.next() < 4:
            self.fail("iter (0)")
        else:
            self.assertTrue(True)
        if 0 < i.next() < 4:
            self.assertTrue(True)
        else:
            self.fail("iter (2)")
        if 0 < i.next() < 4:
            self.fail("iter (4)")
        else:
            self.assertTrue(True)

    def testWhileStatement(self):
        while([]):
            self.fail("Empty lists should not evaluate to True in While")
            break;
        while([1]):
            return
        self.fail("None-empty lists should evaluate to True in While")

    def testLogic(self):

        x = 0 or None
        self.assertEqual(x, None, "0 or None should be None not %s" % repr(x) )

        x = None and None
        self.assertEqual(x, None, "None or None should be None not %s" % repr(x) )

        x = False or None
        self.assertEqual(x, None, "False or None should be None not %s" % repr(x) )

        self.assertTrue((1 or 2) is 1, "(1 or 2) is 1")
        self.assertTrue((0 or 2) is 2, "(0 or 2) is 2")
        self.assertTrue((False or 0) is 0, "(False or 0) is 0")
        self.assertTrue((0 or False) is False, "(0 or False) is False")
        self.assertTrue((0 and 2) is 0, "(0 and 2) is 0")
        self.assertTrue((1 and 2) is 2, "(1 and 2) is 2")
        self.assertTrue((2 and 1) is 1, "(2 and 1) is 1")
        self.assertTrue(([] and 2) == [], "([] and 2) == []")
        try:
            self.assertTrue(({} and 2) == {}, "({} and 2) == {}")
        except:
            self.fail("Unexpected error on '({} and 2) == {}'")
        try:
            self.assertTrue((0 or False or {} or []) == [], "((0 or False or {} or []) == []")
        except:
            self.fail("Unexpected error on '(0 or False or {} or []) == []'")
        f = None
        try:
            self.assertTrue((f and f.test()) == None, "(f and f.test()) == None")
        except:
            self.fail("Unexpected error on '(f and f.test()) == None'")

        self.assertTrue(bool(None) is False, "bool(None) is False")
        self.assertTrue(bool(False) is False, "bool(False) is False")
        self.assertTrue(bool(0) is False, "bool(0) is False")
        self.assertTrue(bool(0.0) is False, "bool(0.0) is False")
        self.assertTrue(bool('') is False, "bool('') is False")
        self.assertTrue(bool([]) is False, "bool('') is False")
        self.assertTrue(bool({}) is False, "bool('') is False")

        self.assertTrue(not None is True, "not None is True")
        self.assertTrue(not False is True, "not False is True")
        self.assertTrue(not 0 is True, "not 0 is True")
        self.assertTrue(not 0.0 is True, "not 0.0 is True")
        self.assertTrue(not '' is True, "not '' is True")
        self.assertTrue(not [] is True, "not '' is True")
        self.assertTrue(not {} is True, "not '' is True")

        d = {'hello': 5}
        d2 = d or {}
        try:
            tst = d == d2
            self.assertTrue(tst, "#297 -non-empty object or {} should return the object")
        except TypeError:
            self.fail("#297 TypeError should not have been thrown")

        d = {}
        d2 = d or 5
        try:
            tst = d2 == 5
            self.assertTrue(tst, "#297 'empty object or 5' should return 5")
        except TypeError:
            self.fail("#297 TypeError should not have been thrown")

<commit_msg>Even more boolean checks<commit_after>from UnitTest import UnitTest

class Stupid:
    pass

class Foo:

    def __init__(self, v):
        self._v = v

    def __nonzero__(self):
        return self._v>0

    def __len__(self):
        return 1

class Bar:

    def __init__(self, v):
        self._v = v

    def __len__(self):
        return self._v

class BoolTest(UnitTest):

    def testBaseTypes(self):
        # meta test first
        self.assertTrue(True)
        self.assertFalse(False)

        # booleans
        self.assertTrue(bool(True))
        self.assertFalse(bool(False))
        self.assertTrue(True == True)
        self.assertFalse(False == True)

        # ints
        self.assertTrue(bool(1))
        self.assertFalse(bool(0))
        self.assertTrue(1)
        self.assertFalse(0)

        # strings
        self.assertTrue(bool('a'))
        self.assertFalse(bool(''))
        self.assertTrue('a')
        self.assertFalse('')

    def testObjects(self):

        # objects
        self.assertTrue(bool(Stupid()))
        self.assertTrue(Stupid())

        # __nonzero__ has precedence
        self.assertFalse(bool(Foo(0)))
        self.assertTrue(bool(Foo(1)))
        self.assertFalse(Foo(0))
        self.assertTrue(Foo(1))

        # __len__ is used secondary
        self.assertFalse(bool(Bar(0)))
        self.assertTrue(bool(Bar(1)))
        self.assertFalse(Bar(0))
        self.assertTrue(Bar(1))

        # lists
        self.assertFalse(bool([]))
        self.assertTrue(bool([1]))
        self.assertFalse([])
        self.assertTrue([1])

        # dicts
        self.assertFalse(bool({}))
        self.assertTrue(bool({'x':1}))
        self.assertFalse({})
        self.assertTrue({'x':1})

    def testIfStatement(self):

        if({}):
            self.fail("Empty dict should not evaluate to True in If")
        else:
            self.assertTrue(True)
        if({1: 5}):
            self.assertTrue(True)
        else:
            self.fail("Non-empty dict should evaluate to True in If")
        if([]):
            self.fail("Empty dict should not evaluate to True in If")
        else:
            self.assertTrue(True)
        if([1]):
            self.assertTrue(True)
        else:
            self.fail("Non-empty lists should evaluate to True in If")
        if not []:
            self.assertTrue(True)
        else:
            self.fail("Non empty lists should not evaluate to True in If")
        if [1] and {}:
            self.fail("'[1] and {}' shoul evaluate to False")
        else:
            self.assertTrue(True)
        if [] or not {}:
            self.assertTrue(True)
        else:
            self.fail("'[] or not {}' shoul evaluate to True")
        if [] and not {}:
            self.fail("'[] and not {}' shoul evaluate to False")
        else:
            self.assertTrue(True)
        if 0 < 1 < 2:
            self.assertTrue(True)
        else:
            self.fail("if 0 < 1 < 2")
        i = [0, 2, 4]
        i = i.__iter__();
        if 0 < i.next() < 4:
            self.fail("iter (0)")
        else:
            self.assertTrue(True)
        if 0 < i.next() < 4:
            self.assertTrue(True)
        else:
            self.fail("iter (2)")
        if 0 < i.next() < 4:
            self.fail("iter (4)")
        else:
            self.assertTrue(True)

    def testWhileStatement(self):
        while([]):
            self.fail("Empty lists should not evaluate to True in While")
            break;
        while([1]):
            return
        self.fail("None-empty lists should evaluate to True in While")

    def testLogic(self):

        x = 0 or None
        self.assertEqual(x, None, "0 or None should be None not %s" % repr(x) )

        x = None and None
        self.assertEqual(x, None, "None or None should be None not %s" % repr(x) )

        x = False or None
        self.assertEqual(x, None, "False or None should be None not %s" % repr(x) )

        self.assertTrue((1 or 2) is 1, "(1 or 2) is 1")
        self.assertTrue((0 or 2) is 2, "(0 or 2) is 2")
        self.assertTrue((False or 0) is 0, "(False or 0) is 0")
        self.assertTrue((0 or False) is False, "(0 or False) is False")
        self.assertTrue((0 and 2) is 0, "(0 and 2) is 0")
        self.assertTrue((1 and 2) is 2, "(1 and 2) is 2")
        self.assertTrue((2 and 1) is 1, "(2 and 1) is 1")
        self.assertTrue(([] and 2) == [], "([] and 2) == []")
        try:
            self.assertTrue(({} and 2) == {}, "({} and 2) == {}")
        except:
            self.fail("Unexpected error on '({} and 2) == {}'")
        try:
            self.assertTrue((0 or False or {} or []) == [], "((0 or False or {} or []) == []")
        except:
            self.fail("Unexpected error on '(0 or False or {} or []) == []'")
        f = None
        try:
            self.assertTrue((f and f.test()) == None, "(f and f.test()) == None")
        except:
            self.fail("Unexpected error on '(f and f.test()) == None'")

        self.assertTrue(bool(None) is False, "bool(None) is False")
        self.assertTrue(bool(False) is False, "bool(False) is False")
        self.assertTrue(bool(0) is False, "bool(0) is False")
        self.assertTrue(bool(0.0) is False, "bool(0.0) is False")
        self.assertTrue(bool('') is False, "bool('') is False")
        self.assertTrue(bool([]) is False, "bool('') is False")
        self.assertTrue(bool({}) is False, "bool('') is False")

        self.assertTrue(not None is True, "not None is True")
        self.assertTrue(not False is True, "not False is True")
        self.assertTrue(not 0 is True, "not 0 is True")
        self.assertTrue(not 0.0 is True, "not 0.0 is True")
        self.assertTrue(not '' is True, "not '' is True")
        self.assertTrue(not [] is True, "not '' is True")
        self.assertTrue(not {} is True, "not '' is True")

        self.assertTrue(bool(1) is True, "bool('') is True")
        self.assertTrue(bool(1.0) is True, "bool('') is True")
        self.assertTrue(bool('a') is True, "bool('') is True")
        self.assertTrue(bool([1]) is True, "bool('') is True")
        self.assertTrue(bool({'a':1}) is True, "bool('') is True")

        self.assertTrue(not 1 is False, "not '' is False")
        self.assertTrue(not 1.0 is False, "not '' is False")
        self.assertTrue(not 'a' is False, "not '' is False")
        self.assertTrue(not [1] is False, "not '' is False")
        self.assertTrue(not {'a':1} is False, "not '' is False")

        d = {'hello': 5}
        d2 = d or {}
        try:
            tst = d == d2
            self.assertTrue(tst, "#297 -non-empty object or {} should return the object")
        except TypeError:
            self.fail("#297 TypeError should not have been thrown")

        d = {}
        d2 = d or 5
        try:
            tst = d2 == 5
            self.assertTrue(tst, "#297 'empty object or 5' should return 5")
        except TypeError:
            self.fail("#297 TypeError should not have been thrown")

<|endoftext|>


<commit_before>"""encrypt_secrets

Revision ID: 3bac7f8ccfdb
Revises: 291237183b82
Create Date: 2019-01-14 17:35:58.872052

"""

# revision identifiers, used by Alembic.
revision = '3bac7f8ccfdb'
down_revision = '291237183b82'

from alembic import op, context
import sqlalchemy as sa

def upgrade():
    from inbox.config import config
    import nacl.secret
    import nacl.utils
    from inbox.ignition import engine_manager
    from inbox.models.session import session_scope

    shard_id = int(context.get_x_argument(as_dictionary=True).get('shard_id'))
    engine = engine_manager.engines[shard_id]
    Base = sa.ext.declarative.declarative_base()
    Base.metadata.reflect(engine)

    class Secret(Base):
        __table__ = Base.metadata.tables['secret']

    class GenericAccount(Base):
        __table__ = Base.metadata.tables['genericaccount']

    with session_scope(shard_id, versioned=False) as db_session:
        secrets = db_session.query(Secret).filter(
            Secret._secret.isnot(None),
            Secret.encryption_scheme == 0).all()

        # Join on the genericaccount and optionally easaccount tables to
        # determine which secrets should have type 'password'.
        generic_query = db_session.query(Secret.id).join(
            GenericAccount, Secret.id == GenericAccount.password_id)
        password_secrets = [id_ for id_, in generic_query]
        if engine.has_table('easaccount'):
            class EASAccount(Base):
                __table__ = Base.metadata.tables['easaccount']

            eas_query = db_session.query(Secret.id).join(
                EASAccount).filter(Secret.id == EASAccount.password_id)
            password_secrets.extend([id_ for id_, in eas_query])

        for s in secrets:
            plain = s._secret.encode('utf-8') if isinstance(s._secret, unicode) \
                else s._secret
            if config.get_required('ENCRYPT_SECRETS'):

                s._secret = nacl.secret.SecretBox(
                    key=config.get_required('SECRET_ENCRYPTION_KEY'),
                    encoder=nacl.encoding.HexEncoder
                ).encrypt(
                    plaintext=plain,
                    nonce=nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE))

                # 1 is EncryptionScheme.SECRETBOX_WITH_STATIC_KEY
                s.encryption_scheme = 1
            else:
                s._secret = plain

            if s.id in password_secrets:
                s.type = 'password'
            else:
                s.type = 'token'

            db_session.add(s)

        db_session.commit()

def downgrade():
    pass
<commit_msg>Cleanup<commit_after>"""encrypt_secrets

Revision ID: 3bac7f8ccfdb
Revises: 291237183b82
Create Date: 2019-01-14 17:35:58.872052

"""

# revision identifiers, used by Alembic.
revision = '3bac7f8ccfdb'
down_revision = '291237183b82'

from alembic import op, context
import sqlalchemy as sa

def upgrade():
    from inbox.config import config
    import nacl.secret
    import nacl.utils
    from inbox.ignition import engine_manager
    from inbox.models.session import session_scope

    shard_id = int(context.get_x_argument(as_dictionary=True).get('shard_id'))
    engine = engine_manager.engines[shard_id]
    Base = sa.ext.declarative.declarative_base()
    Base.metadata.reflect(engine)

    class Secret(Base):
        __table__ = Base.metadata.tables['secret']

    with session_scope(shard_id, versioned=False) as db_session:
        secrets = db_session.query(Secret).filter(
            Secret._secret.isnot(None),
            Secret.encryption_scheme == 0).all()

        for s in secrets:
            plain = s._secret.encode('utf-8') if isinstance(s._secret, unicode) \
                else s._secret
            if config.get_required('ENCRYPT_SECRETS'):

                s._secret = nacl.secret.SecretBox(
                    key=config.get_required('SECRET_ENCRYPTION_KEY'),
                    encoder=nacl.encoding.HexEncoder
                ).encrypt(
                    plaintext=plain,
                    nonce=nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE))

                # 1 is EncryptionScheme.SECRETBOX_WITH_STATIC_KEY
                s.encryption_scheme = 1
            else:
                s._secret = plain

            db_session.add(s)

        db_session.commit()

def downgrade():
    pass
<|endoftext|>


<commit_before>"""
A model (``RegistrationProfile``) for storing user-registration data,
and an associated custom manager (``RegistrationManager``).

"""

import datetime, random, re, hashlib

from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils import timezone

SHA1_RE = re.compile('^[a-f0-9]{40}$')

class RegistrationManager(models.Manager):
    """
    Custom manager for the ``RegistrationProfile`` model.

    The methods defined here provide shortcuts for account creation
    and activation (including generation and emailing of activation
    keys), and for cleaning out expired inactive accounts.

    """
    def activate_user(self, activation_key):
        """
        Validates an activation key and activates the corresponding
        ``User`` if valid.

        If the key is valid and has not expired, returns the ``User``
        after activating.

        If the key is not valid or has expired, returns ``False``.

        If the key is valid but the ``User`` is already active,
        returns the ``User``.

        """
        # Make sure the key we're trying conforms to the pattern of a
        # SHA1 hash; if it doesn't, no point trying to look it up in
        # the database.
        if SHA1_RE.search(activation_key):
            try:
                profile = self.get(activation_key=activation_key)
            except self.model.DoesNotExist:
                return False
            if not profile.activation_key_expired():
                user = profile.user
                user.is_active = True
                user.save()
                return user
        return False

    def create_inactive_user(self, username, password, email,
                             firstname="", lastname="",
                             send_email=True, profile_callback=None,
                             is_researcher=False):
        """
        Creates a new, inactive ``User``, generates a
        ``RegistrationProfile`` and emails its activation key to the
        ``User``. Returns the new ``User``.

        To disable the email, call with ``send_email=False``.

        To enable creation of a custom user profile along with the
        ``User`` (e.g., the model specified in the
        ``AUTH_PROFILE_MODULE`` setting), define a function which
        knows how to create and save an instance of that model with
        appropriate default values, and pass it as the keyword
        argument ``profile_callback``. This function should accept one
        keyword argument:

        ``user``
            The ``User`` to relate the profile to.

        """
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = False
        new_user.first_name = firstname
        new_user.last_name = lastname
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if profile_callback is not None:
            profile_callback(user=new_user)

        if send_email:
            from django.core.mail import send_mail
            current_site = Site.objects.get_current()

            subject = render_to_string('registration/activation_email_subject.txt',
                                       { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('registration/activation_email.txt',
                                       { 'activation_key': registration_profile.activation_key,
                                         'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                         'is_researcher': is_researcher,
                                         'site': current_site })

            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user

    def create_profile(self, user):
        """
        Creates a ``RegistrationProfile`` for a given
        ``User``. Returns the ``RegistrationProfile``.

        The activation key for the ``RegistrationProfile`` will be a
        SHA1 hash, generated from a combination of the ``User``'s
        username and a random salt.

        """
        salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
        activation_key = hashlib.sha1(salt+user.username).hexdigest()
        return self.create(user=user,
                           activation_key=activation_key)

    def delete_expired_users(self):
        """
        Removes expired instances of ``RegistrationProfile`` and their
        associated ``User``s.

        Accounts to be deleted are identified by searching for
        instances of ``RegistrationProfile`` with expired activation
        keys, and then checking to see if their associated ``User``
        instances have the field ``is_active`` set to ``False``; any
        ``User`` who is both inactive and has an expired activation
        key will be deleted.

        It is recommended that this method be executed regularly as
        part of your routine site maintenance; the file
        ``bin/delete_expired_users.py`` in this application provides a
        standalone script, suitable for use as a cron job, which will
        call this method.

        Regularly clearing out accounts which have never been
        activated serves two useful purposes:

        1. It alleviates the ocasional need to reset a
           ``RegistrationProfile`` and/or re-send an activation email
           when a user does not receive or does not act upon the
           initial activation email; since the account will be
           deleted, the user will be able to simply re-register and
           receive a new activation key.

        2. It prevents the possibility of a malicious user registering
           one or more accounts and never activating them (thus
           denying the use of those usernames to anyone else); since
           those accounts will be deleted, the usernames will become
           available for use again.

        If you have a troublesome ``User`` and wish to disable their
        account while keeping it in the database, simply delete the
        associated ``RegistrationProfile``; an inactive ``User`` which
        does not have an associated ``RegistrationProfile`` will not
        be deleted.

        """
        for profile in self.all():
            if profile.activation_key_expired():
                user = profile.user
                if not user.is_active:
                    user.delete()

class RegistrationProfile(models.Model):
    """
    A simple profile which stores an activation key for use during
    user account registration.

    Generally, you will not want to interact directly with instances
    of this model; the provided manager includes methods
    for creating and activating new accounts, as well as for cleaning
    out accounts which have never been activated.

    While it is possible to use this model as the value of the
    ``AUTH_PROFILE_MODULE`` setting, it's not recommended that you do
    so. This model's sole purpose is to store data temporarily during
    account registration and activation, and a mechanism for
    automatically creating an instance of a site-specific profile
    model is provided via the ``create_inactive_user`` on
    ``RegistrationManager``.

    """
    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
    activation_key = models.CharField(_('activation key'), max_length=40)

    objects = RegistrationManager()

    class Meta:
        verbose_name = _('registration profile')
        verbose_name_plural = _('registration profiles')

    class Admin:
        list_display = ('__str__', 'activation_key_expired')
        search_fields = ('user__username', 'user__first_name')

    def __unicode__(self):
        return u"Registration information for %s" % self.user

    def activation_key_expired(self):
        """
        Determines whether this ``RegistrationProfile``'s activation
        key has expired.

        Returns ``True`` if the key has expired, ``False`` otherwise.

        Key expiration is determined by the setting
        ``ACCOUNT_ACTIVATION_DAYS``, which should be the number of
        days a key should remain valid after an account is registered.

        """
        expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
        return self.user.date_joined + expiration_date <= timezone.now()
    activation_key_expired.boolean = True

from django.contrib.auth import models as authmodels

import string

def t(message):
    """Replace $country and $language in message with dat from settings"""

    tpl = string.Template(message)
    return tpl.substitute(country=settings.COUNTRY_NAME, language=settings.LANGUAGE_NAME)

backgroundChoices = ((0, 'deaf community'),
            	      (1, t('$language teacher')),
                     (2, 'teacher of the deaf'),
                     (3, 'parent of a deaf child'),
                     (4, 'sign language interpreter'),
                     (5, 'school or college student (under 18)'),
                     (6, t('student learning $language')),
                     (9, 'university student (taught, e.g. BA, BSc, MA, Msc)'),
                     (10, 'university student (research, e.g. PhD)'),
                     (8, 'university academic/research staff'),
                     (7, 'other'),
                     )

learnedChoices = ((0, 'Not Applicable'),
           (1, 'At home from my parent(s)'),
           (2, 'At kindergarten or at the beginning of primary school'),
           (3, 'At primary school'),
           (4, 'At high school'),
           (5, 'After I left school'),
           )

schoolChoices = ((0, 'a deaf school (boarder)'),
                 (1, 'a deaf school (day student)'),
                 (2, 'a deaf classroom or unit in a hearing school'),
                 (3, 'a regular classroom in a hearing school'),
                 )

teachercommChoices = ((0, 'mostly oral'),
                      (1, 'mostly Signed English'),
                      (2, t('mostly sign language ($language)')),
                      (3, 'mostly fingerspelling'),
                      (4, 'N/A, I am hearing')
                      )

class UserProfile(models.Model):
    """Extended profile for users of the site"""

    user = models.ForeignKey(authmodels.User, unique=True)
    yob = models.IntegerField("When were you born?")
    australian = models.BooleanField(t("Do you live in $country?"))
    postcode = models.CharField(t("If you live in $country, what is your postcode?"), max_length=20, blank=True)
    background = models.CommaSeparatedIntegerField("What is your background?", max_length=20, choices=backgroundChoices)
    researcher_credentials = models.TextField(t("Research credentials"))
    auslan_user = models.BooleanField(t("Do you use $language?"))
    learned = models.IntegerField(t("If you use $language, when did you learn sign language?"),
                                  choices=learnedChoices)
    deaf = models.BooleanField("Are you a deaf person?")
    schooltype = models.IntegerField("What sort of school do you (or did you) attend?",
                                     choices=schoolChoices)
    school = models.CharField("Which school do you (or did you) attend?", max_length=50, blank=True)
    teachercomm = models.IntegerField("How do (or did) your teachers communicate with you?",
                                      choices=teachercommChoices)

    @staticmethod
    def best_describes_you_from_background(background):
        result = []
        try:
            indices = background.split(",")
            for index in indices:
                i = int(''.join(n for n in index if n.isdigit()))
                result.append(dict(backgroundChoices)[i])
        except:
            result = ['(unknown)']
        return ", ".join(result)

    def best_describes_you(self):
        "Return the background in a readable form"
        return UserProfile.best_describes_you_from_background(self.background)

    @staticmethod
    def is_researcher_from_background(background):
        return 'research' in UserProfile.best_describes_you_from_background(background)

    def is_researcher(self):
        "True if this person has picked a background that includes the word research"
        return UserProfile.is_researcher_from_background(self.background)

    class Admin:
        list_display = ['user', 'deaf', 'auslan_user']

<commit_msg>Add the credentials migration even though we're not using it at the moment.<commit_after>"""
A model (``RegistrationProfile``) for storing user-registration data,
and an associated custom manager (``RegistrationManager``).

"""

import datetime, random, re, hashlib

from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils import timezone

SHA1_RE = re.compile('^[a-f0-9]{40}$')

class RegistrationManager(models.Manager):
    """
    Custom manager for the ``RegistrationProfile`` model.

    The methods defined here provide shortcuts for account creation
    and activation (including generation and emailing of activation
    keys), and for cleaning out expired inactive accounts.

    """
    def activate_user(self, activation_key):
        """
        Validates an activation key and activates the corresponding
        ``User`` if valid.

        If the key is valid and has not expired, returns the ``User``
        after activating.

        If the key is not valid or has expired, returns ``False``.

        If the key is valid but the ``User`` is already active,
        returns the ``User``.

        """
        # Make sure the key we're trying conforms to the pattern of a
        # SHA1 hash; if it doesn't, no point trying to look it up in
        # the database.
        if SHA1_RE.search(activation_key):
            try:
                profile = self.get(activation_key=activation_key)
            except self.model.DoesNotExist:
                return False
            if not profile.activation_key_expired():
                user = profile.user
                user.is_active = True
                user.save()
                return user
        return False

    def create_inactive_user(self, username, password, email,
                             firstname="", lastname="",
                             send_email=True, profile_callback=None,
                             is_researcher=False):
        """
        Creates a new, inactive ``User``, generates a
        ``RegistrationProfile`` and emails its activation key to the
        ``User``. Returns the new ``User``.

        To disable the email, call with ``send_email=False``.

        To enable creation of a custom user profile along with the
        ``User`` (e.g., the model specified in the
        ``AUTH_PROFILE_MODULE`` setting), define a function which
        knows how to create and save an instance of that model with
        appropriate default values, and pass it as the keyword
        argument ``profile_callback``. This function should accept one
        keyword argument:

        ``user``
            The ``User`` to relate the profile to.

        """
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = False
        new_user.first_name = firstname
        new_user.last_name = lastname
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if profile_callback is not None:
            profile_callback(user=new_user)

        if send_email:
            from django.core.mail import send_mail
            current_site = Site.objects.get_current()

            subject = render_to_string('registration/activation_email_subject.txt',
                                       { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('registration/activation_email.txt',
                                       { 'activation_key': registration_profile.activation_key,
                                         'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                         'is_researcher': is_researcher,
                                         'site': current_site })

            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user

    def create_profile(self, user):
        """
        Creates a ``RegistrationProfile`` for a given
        ``User``. Returns the ``RegistrationProfile``.

        The activation key for the ``RegistrationProfile`` will be a
        SHA1 hash, generated from a combination of the ``User``'s
        username and a random salt.

        """
        salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
        activation_key = hashlib.sha1(salt+user.username).hexdigest()
        return self.create(user=user,
                           activation_key=activation_key)

    def delete_expired_users(self):
        """
        Removes expired instances of ``RegistrationProfile`` and their
        associated ``User``s.

        Accounts to be deleted are identified by searching for
        instances of ``RegistrationProfile`` with expired activation
        keys, and then checking to see if their associated ``User``
        instances have the field ``is_active`` set to ``False``; any
        ``User`` who is both inactive and has an expired activation
        key will be deleted.

        It is recommended that this method be executed regularly as
        part of your routine site maintenance; the file
        ``bin/delete_expired_users.py`` in this application provides a
        standalone script, suitable for use as a cron job, which will
        call this method.

        Regularly clearing out accounts which have never been
        activated serves two useful purposes:

        1. It alleviates the ocasional need to reset a
           ``RegistrationProfile`` and/or re-send an activation email
           when a user does not receive or does not act upon the
           initial activation email; since the account will be
           deleted, the user will be able to simply re-register and
           receive a new activation key.

        2. It prevents the possibility of a malicious user registering
           one or more accounts and never activating them (thus
           denying the use of those usernames to anyone else); since
           those accounts will be deleted, the usernames will become
           available for use again.

        If you have a troublesome ``User`` and wish to disable their
        account while keeping it in the database, simply delete the
        associated ``RegistrationProfile``; an inactive ``User`` which
        does not have an associated ``RegistrationProfile`` will not
        be deleted.

        """
        for profile in self.all():
            if profile.activation_key_expired():
                user = profile.user
                if not user.is_active:
                    user.delete()

class RegistrationProfile(models.Model):
    """
    A simple profile which stores an activation key for use during
    user account registration.

    Generally, you will not want to interact directly with instances
    of this model; the provided manager includes methods
    for creating and activating new accounts, as well as for cleaning
    out accounts which have never been activated.

    While it is possible to use this model as the value of the
    ``AUTH_PROFILE_MODULE`` setting, it's not recommended that you do
    so. This model's sole purpose is to store data temporarily during
    account registration and activation, and a mechanism for
    automatically creating an instance of a site-specific profile
    model is provided via the ``create_inactive_user`` on
    ``RegistrationManager``.

    """
    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
    activation_key = models.CharField(_('activation key'), max_length=40)

    objects = RegistrationManager()

    class Meta:
        verbose_name = _('registration profile')
        verbose_name_plural = _('registration profiles')

    class Admin:
        list_display = ('__str__', 'activation_key_expired')
        search_fields = ('user__username', 'user__first_name')

    def __unicode__(self):
        return u"Registration information for %s" % self.user

    def activation_key_expired(self):
        """
        Determines whether this ``RegistrationProfile``'s activation
        key has expired.

        Returns ``True`` if the key has expired, ``False`` otherwise.

        Key expiration is determined by the setting
        ``ACCOUNT_ACTIVATION_DAYS``, which should be the number of
        days a key should remain valid after an account is registered.

        """
        expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
        return self.user.date_joined + expiration_date <= timezone.now()
    activation_key_expired.boolean = True

from django.contrib.auth import models as authmodels

import string

def t(message):
    """Replace $country and $language in message with dat from settings"""

    tpl = string.Template(message)
    return tpl.substitute(country=settings.COUNTRY_NAME, language=settings.LANGUAGE_NAME)

backgroundChoices = ((0, 'deaf community'),
            	      (1, t('$language teacher')),
                     (2, 'teacher of the deaf'),
                     (3, 'parent of a deaf child'),
                     (4, 'sign language interpreter'),
                     (5, 'school or college student (under 18)'),
                     (6, t('student learning $language')),
                     (9, 'university student (taught, e.g. BA, BSc, MA, Msc)'),
                     (10, 'university student (research, e.g. PhD)'),
                     (8, 'university academic/research staff'),
                     (7, 'other'),
                     )

learnedChoices = ((0, 'Not Applicable'),
           (1, 'At home from my parent(s)'),
           (2, 'At kindergarten or at the beginning of primary school'),
           (3, 'At primary school'),
           (4, 'At high school'),
           (5, 'After I left school'),
           )

schoolChoices = ((0, 'a deaf school (boarder)'),
                 (1, 'a deaf school (day student)'),
                 (2, 'a deaf classroom or unit in a hearing school'),
                 (3, 'a regular classroom in a hearing school'),
                 )

teachercommChoices = ((0, 'mostly oral'),
                      (1, 'mostly Signed English'),
                      (2, t('mostly sign language ($language)')),
                      (3, 'mostly fingerspelling'),
                      (4, 'N/A, I am hearing')
                      )

class UserProfile(models.Model):
    """Extended profile for users of the site"""

    user = models.ForeignKey(authmodels.User, unique=True)
    yob = models.IntegerField("When were you born?")
    australian = models.BooleanField(t("Do you live in $country?"))
    postcode = models.CharField(t("If you live in $country, what is your postcode?"), max_length=20, blank=True)
    background = models.CommaSeparatedIntegerField("What is your background?", max_length=20, choices=backgroundChoices)
    researcher_credentials = models.TextField(t("Research credentials"), default='')
    auslan_user = models.BooleanField(t("Do you use $language?"))
    learned = models.IntegerField(t("If you use $language, when did you learn sign language?"),
                                  choices=learnedChoices)
    deaf = models.BooleanField("Are you a deaf person?")
    schooltype = models.IntegerField("What sort of school do you (or did you) attend?",
                                     choices=schoolChoices)
    school = models.CharField("Which school do you (or did you) attend?", max_length=50, blank=True)
    teachercomm = models.IntegerField("How do (or did) your teachers communicate with you?",
                                      choices=teachercommChoices)

    @staticmethod
    def best_describes_you_from_background(background):
        result = []
        try:
            indices = background.split(",")
            for index in indices:
                i = int(''.join(n for n in index if n.isdigit()))
                result.append(dict(backgroundChoices)[i])
        except:
            result = ['(unknown)']
        return ", ".join(result)

    def best_describes_you(self):
        "Return the background in a readable form"
        return UserProfile.best_describes_you_from_background(self.background)

    @staticmethod
    def is_researcher_from_background(background):
        return 'research' in UserProfile.best_describes_you_from_background(background)

    def is_researcher(self):
        "True if this person has picked a background that includes the word research"
        return UserProfile.is_researcher_from_background(self.background)

    class Admin:
        list_display = ['user', 'deaf', 'auslan_user']

<|endoftext|>


<commit_before># Convenience imports from pexpect
from __future__ import absolute_import
from pexpect import which, EOF, TIMEOUT
<commit_msg>Add missing import in pexpect<commit_after># Convenience imports from pexpect
from __future__ import absolute_import
from pexpect import spawn, which, EOF, TIMEOUT
<|endoftext|>


<commit_before>import os

def is_container() -> bool:
    path = "/proc/self/cgroup"
    return os.path.exists("/.dockerenv") or os.path.isfile(path) and any("docker" in line for line in open(path))

def cpu_count() -> int:
    if is_container():
        if os.path.isfile("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"):
            cpu_quota = int(open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").read().rstrip())
        # print(cpu_quota) # Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
        if cpu_quota != -1 and os.path.isfile("/sys/fs/cgroup/cpu/cpu.cfs_period_us"):
            cpu_period = int(open("/sys/fs/cgroup/cpu/cpu.cfs_period_us").read().rstrip())
            # print(cpu_period)
            avail_cpu = int(cpu_quota / cpu_period)
            # Divide quota by period and you should get num of allotted CPU to the container,rounded down if fractional.
        elif os.path.isfile("/sys/fs/cgroup/cpu/cpu.shares"):
            cpu_shares = int(open("/sys/fs/cgroup/cpu/cpu.shares").read().rstrip())
            # print(cpu_shares) # For AWS, gives correct value * 1024.
            avail_cpu = int(cpu_shares / 1024)
        return avail_cpu
    else:
        return os.cpu_count()
<commit_msg>Helpers to calculate memory + cpu correctly across host and container systems<commit_after>import os
import sys
from dataclasses import dataclass

import psutil
from cgroupspy.trees import Tree

from paralleldomain.utilities.any_path import AnyPath

def is_container() -> bool:
    """Returns `True` if process is running inside a (Docker) container."""
    cgroup_path = AnyPath("/proc/self/cgroup")
    return (
        AnyPath("/.dockerenv").exists()
        or cgroup_path.is_file()
        and any("docker" in line for line in cgroup_path.open(mode="r"))
    )

def cpu_count() -> int:
    """Returns CPU count. If process is running inside a (Docker) container,
    it returns the allocated virtual CPU count."""
    if is_container():
        cgroup_tree = Tree()
        cpu_controller = cgroup_tree.get_node_by_path("/cpu/").controller
        if cpu_controller.cfs_quota_us is None:  # running inside AWS-hosted container
            return int(cpu_controller.shares / 1024)
        else:  # running locally hosted container
            return int(cpu_controller.cfs_quota_us / cpu_controller.cfs_period_us)
    else:
        return os.cpu_count()

@dataclass
class Memory:
    total: int
    used: int

    @property
    def available(self) -> int:
        return self.total - self.used

def virtual_memory() -> Memory:
    """Returns total, used and available memory. If process is running inside a (Docker) container,
    it returns the allocated memory limit, or pyhsical host memory if no explicit limit has been set."""
    if is_container():
        cgroup_tree = Tree()
        memory_controller = cgroup_tree.get_node_by_path("/memory/").controller

        memory_limit = memory_controller.limit_in_bytes
        if memory_limit == (sys.maxsize - sys.maxsize % 4096):  # no container limit - take host physical limit
            memory_limit = psutil.virtual_memory().total
        return Memory(total=memory_limit, used=memory_controller.usage_in_bytes)
    else:
        psutil_vm = psutil.virtual_memory()
        return Memory(total=psutil_vm.total, used=psutil_vm.used)
<|endoftext|>


<commit_before>"""
The ``simpy`` module aggregates SimPy's most used components into a single
namespace. This is purely for convenience. You can of course also access
everything (and more!) via their actual submodules.

The following tables list all of the available components in this module.

{toc}

"""
from pkgutil import extend_path

from simpy.core import Environment
from simpy.rt import RealtimeEnvironment
from simpy.events import Event, Timeout, Process, AllOf, AnyOf, Interrupt
from simpy.resources.resource import (
    Resource, PriorityResource, PreemptiveResource)
from simpy.resources.container import Container
from simpy.resources.store import Store, FilterStore
from simpy.util import test

def compile_toc(entries, section_marker='='):
    """Compiles a list of sections with objects into sphinx formatted
    autosummary directives."""
    toc = ''
    for section, objs in entries:
        toc += '\n\n%s\n%s\n\n' % (section, section_marker * len(section))
        toc += '.. autosummary::\n\n'
        for obj in objs:
            toc += '    ~%s.%s\n' % (obj.__module__, obj.__name__)
    return toc

toc = (
    ('Environments', (
        Environment, RealtimeEnvironment,
    )),
    ('Events', (
        Event, Timeout, Process, AllOf, AnyOf, Interrupt,
    )),
    ('Resources', (
        Resource, PriorityResource, PreemptiveResource, Container, Store,
        FilterStore,
    )),
    ('Miscellaneous', (
        test,
    )),
)

# Use the toc to keep the documentation and the implementation in sync.
__doc__ = __doc__.format(toc=compile_toc(toc))
__all__ = [obj.__name__ for section, objs in toc for obj in objs]

__path__ = extend_path(__path__, __name__)
__version__ = '3.0.6'
<commit_msg>Bump version to 3.0.7.<commit_after>"""
The ``simpy`` module aggregates SimPy's most used components into a single
namespace. This is purely for convenience. You can of course also access
everything (and more!) via their actual submodules.

The following tables list all of the available components in this module.

{toc}

"""
from pkgutil import extend_path

from simpy.core import Environment
from simpy.rt import RealtimeEnvironment
from simpy.events import Event, Timeout, Process, AllOf, AnyOf, Interrupt
from simpy.resources.resource import (
    Resource, PriorityResource, PreemptiveResource)
from simpy.resources.container import Container
from simpy.resources.store import Store, FilterStore
from simpy.util import test

def compile_toc(entries, section_marker='='):
    """Compiles a list of sections with objects into sphinx formatted
    autosummary directives."""
    toc = ''
    for section, objs in entries:
        toc += '\n\n%s\n%s\n\n' % (section, section_marker * len(section))
        toc += '.. autosummary::\n\n'
        for obj in objs:
            toc += '    ~%s.%s\n' % (obj.__module__, obj.__name__)
    return toc

toc = (
    ('Environments', (
        Environment, RealtimeEnvironment,
    )),
    ('Events', (
        Event, Timeout, Process, AllOf, AnyOf, Interrupt,
    )),
    ('Resources', (
        Resource, PriorityResource, PreemptiveResource, Container, Store,
        FilterStore,
    )),
    ('Miscellaneous', (
        test,
    )),
)

# Use the toc to keep the documentation and the implementation in sync.
__doc__ = __doc__.format(toc=compile_toc(toc))
__all__ = [obj.__name__ for section, objs in toc for obj in objs]

__path__ = extend_path(__path__, __name__)
__version__ = '3.0.7'
<|endoftext|>